skinny-framework / skinny-framework

:monorail: "Scala on Rails" - A full-stack web app framework for rapid development in Scala

Home Page:https://skinny-framework.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ORM `joins` functionality doesn't include schema name in primary table from clause

gavares opened this issue · comments

commented

Given two entities

case class Permission(id: Long, subjectId: Subject) extends SkinnyRecord[Permission] {
 override def skinnyCRUDMapper: CRUDFeatureWithId[Long, Permission] = Permission
}

object Permission extends SkinnyCRUDMapper[Permission] {
  override val schemaName = "my_schema"
  override def defaultAlias = createAlias("perm")
  override lazy val columns = autoColumns[Permission]("subject")

  val subject = belongsToWithFk[Subject](
    right = Subject,
    fk = column.subjectId.value,
    merge = (p, s) => p.copy(subject = s.get)
  )

}

And a subject entity:

case class Subject(entityType: String, subjectKey: String, id: Long = -1)
    extends SkinnyRecord[Subject] {
  override def skinnyCRUDMapper: CRUDFeatureWithId[Long, Subject] = Subject
}

object OrgSubject extends SkinnyCRUDMapper[Subject] {
  override val schemaName = Permission.schemaName
  override val tableName = "org_subject"
  override lazy val defaultAlias = createAlias("sub")
  override lazy val columns = autoColumns[Subject]()
  override def useAutoIncrementPrimaryKey = true

Attemping to run:

  Permission.withColumns { p => 
      Permission.joins(Permission.subject).where.eq(p.id, 1)
  }

Generates a query that does not include the schema name for the Permission table but does include the schema name for the Subject table. This causes queries to fail due to table not found:

   select perm.id, sub.entity_type, sub.subject_key, sub.id from permission as perm left join my_schema.subject  as sub on perm.subject_id = sub.id where perm.id = 1

In the query above, the from clause should reference the permission table as my_schema.permission instead of just permission.

This behavior happens for all the entity relations I've tried: hasOne, hasMany, belongsTo

I'm currently using skinny-orm: 4.0.0

If I remember correctly, this kind of issue tends to depend on the underlying JDBC driver's behavior. I am open to merging external contributions to fix it as long as the change does not bring any breaking changes to existing apps.