Join Slick 3.0多对多呼叫
从这篇文章中获得灵感: 我的情况大致相同,只是有一些小的例外Join Slick 3.0多对多呼叫,join,slick,Join,Slick,从这篇文章中获得灵感: 我的情况大致相同,只是有一些小的例外 def testManyToMany(): Unit = db withSession { object A extends Table[(Int, String)]("a") { def id = column[Int]("id", O.PrimaryKey) def s = column[String]("s") def * = id ~ s def bs = AToB.
def testManyToMany(): Unit = db withSession {
object A extends Table[(Int, String)]("a") {
def id = column[Int]("id", O.PrimaryKey)
def s = column[String]("s")
def * = id ~ s
def bs = AToB.filter(_.aId === id).flatMap(_.bFK)
// note I have another many-to-many join
def cs = AtoC.filter(_cId === id).flatMap(_.aFK)
}
object B extends Table[(Int, String)]("b") {
def id = column[Int]("id", O.PrimaryKey)
def s = column[String]("s")
def * = id ~ s
def as = AToB.filter(_.bId === id).flatMap(_.aFK)
}
object AToB extends Table[(Int, Int)]("a_to_b") {
def aId = column[Int]("a")
def bId = column[Int]("b")
def * = aId ~ bId
def aFK = foreignKey("a_fk", aId, A)(a => a.id)
def bFK = foreignKey("b_fk", bId, B)(b => b.id)
}
object AToC extends Table[(Int, Int)]("a_to_c") {
def aId = column[Int]("a")
def cId = column[Int]("c")
def * = aId ~ cId
def aFK = foreignKey("a_fk", aId, A)(a => a.id)
def cFK = foreignKey("c_fk", cId, C)(c => c.id)
}
}
Now when I want to fetch all A's by id, I would also want to fetch it associations in B and C, I would do something like:
for {
a <- A if a.id >= 2
aToB <- AToB if aToB.aId === a.id
b <- B if b.id === aToB.bId
} yield (a.s, b.s)
How can I include the join to the C table? Is having something like this correct?
for {
a <- A if a.id >= 2
aToB <- AToB if aToB.aId === a.id
aToC <- AToC if aToC.aId === a.id
b <- B if b.id === aToB.bId
c <- C if c.id === aToC.cId
} yield (a.s, b.s)
def testManyToMany():Unit=db with session{
对象A扩展表[(Int,String)](“A”){
def id=列[Int](“id”,O.PrimaryKey)
def s=列[字符串](“s”)
def*=id~s
def bs=AToB.filter(u.aId==id).flatMap(u.bFK)
//注意,我还有另一个多对多连接
def cs=AtoC.filter(_cId==id).flatMap(u.aFK)
}
对象B扩展表[(Int,String)](“B”){
def id=列[Int](“id”,O.PrimaryKey)
def s=列[字符串](“s”)
def*=id~s
def as=AToB.filter(u.bId==id).flatMap(u.aFK)
}
对象AToB扩展表[(Int,Int)](“a_到b”){
def aId=列[Int](“a”)
def bId=列[Int](“b”)
def*=aId~bId
def aFK=foreignKey(“a_fk”,aId,a)(a=>a.id)
def bFK=外键(“b_fk”,投标,b)(b=>b.id)
}
对象AToC扩展表[(Int,Int)](“从a到c”){
def aId=列[Int](“a”)
def cId=列[Int](“c”)
def*=aId~cId
def aFK=foreignKey(“a_fk”,aId,a)(a=>a.id)
def cFK=外键(“c_fk”,cId,c)(c=>c.id)
}
}
现在,当我想通过id获取所有A时,我还想获取B和C中的关联,我会做如下操作:
为了{
a=2
阿托布