0

你能告诉我为什么我没有得到我期望得到的内部加入吗?

我有以下表格

case class Ability(id: UUID, can: Boolean, verb: String, subject: String, context: String)

object Abilities extends Table[Ability]("abilities"){
  def id = column[UUID]("id", O.PrimaryKey)
  def can = column[Boolean]("is_can")
  def verb = column[String]("verb")
  def subject = column[String]("subject")
  def context = column[String]("context")

  def * = id ~ can ~ verb ~ subject ~ context <> (Ability, Ability.unapply _)
}

case class Role(id: UUID, name : String)

object Roles extends Table[Role]("roles"){
  def id = column[UUID]("id", O.PrimaryKey)
  def name = column[String]("name")

  def * = id ~ name <> (Role, Role.unapply _)
}

// And join table
case class AbilityRelationship(owner_id: UUID, obj_id: UUID, is_role: Boolean)

object AbilitiesMapping extends Table[AbilityRelationship]("abilities_mapping"){
  def owner_id = column[UUID]("owner_id")
  def obj_id = column[UUID]("obj_id")
  def is_role = column[Boolean]("is_role")

  def * = owner_id ~ obj_id ~ is_role <> (AbilityRelationship, AbilityRelationship.unapply _)
}

我愿意做的是获取Ability特定所有者(无论是用户还是角色)的对象列表。因此,按照文档,我为它编写了以下连接查询

val some_id = role.id
val q2 = for {
  a <- Abilities
  rel <- AbilitiesMapping
  if rel.owner_id === some_id.bind
} yield (a)

q2.selectStatement返回绝对错误的查询。在select x2."id", x2."is_can", x2."verb", x2."subject", x2."context" from "abilities" x2, "abilities_mapping" x3 where x3."owner_id" = ?我的情况下。

应该如何实施?

谢谢。

4

3 回答 3

9

好吧,经过多次尝试,我做到了

  val innerJoin = for {
    (a, rel) <- Abilities innerJoin AbilitiesMapping on (_.id === _.obj_id) if rel.owner_id === some_id.bind
  } yield a

但是 man... typesafe 的文档对于新手来说真的很弱。

于 2013-05-23T07:29:20.980 回答
2

尝试类似:

val q2 = for {
  a <- Abilities
  rel <- AbilitiesMapping
  if a.id == rel.obj_id && rel.owner_id === some_id.bind
} yield (a)

顺便说一句,您知道可以在 Table 对象中注释外键吗?

于 2013-05-23T07:07:57.127 回答
1

尝试这样做作为对鲁斯兰回答的评论,但我只是没有足够的绝地力量:

如果这个脱糖版本有效,你可以试试吗?

val rightSide = AbilitiesMapping.filter(_.owner_id === some_id)    
val innerJoin = (Abilities innerJoin (rightSide) on (
  (l,r) => (l.id === r.obj_id)
).map { case (l, r) => l }
于 2014-03-05T03:36:47.707 回答