1
我想要在查詢端定義一個類型來映射我的聯接,以便我可以避免返回值的元組,我必須手動應用到我的投影案例類帖子查詢。鍵入投影與油滑
給予類似的關係:
case class Parent(id: Int, name: String, extra: String)
class ParentTable(tag: Tag) extends Table[Parent](tag, "parent") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def extra = column[String]("extra")
def * = (id, name, extra) <> (Parent.tupled, Parent.unapply)
}
val parents = TableQuery[ParentTable]
case class Child(id: Int, parentId: Int, name: String, extra: String)
class ChildTable(tag: Tag) extends Table[Child](tag, "child") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def parentId = column[Int]("parent_id")
def parent = foreignKey("parent_fk", parentId, parents)(_.id)
def name = column[String]("name")
def extra = column[String]("extra")
def * = (id, parentId, name, extra) <> (Child.tupled, Child.unapply)
}
val children = TableQuery[ChildTable]
我要投射到一個案例類,如:
case class ChildWithParentName(id: Int, name: String, parentName: String)
的加入和投影的樣子:
val q = for {
c <- children
p <- parents if c.parentId === p.id
} yield (c.id,c.name,p.name)
我把這個在功能中允許參數化children
和parents
。該功能不運行查詢,因爲有時我想.result
,有時我想.result.headOption
,所以我的函數簽名是:
Query[(Rep[Int], Rep[String], Rep[String]), (Int, String, String), Seq]
我想在查詢側的形狀像創建類型:
class ChildParentProjection(val id: Rep[Int],
val name: Rep[String],
val parentName[String])
,這樣我可以得到這樣一個函數簽名:
Query[ChildParentProjection, ChildWithParentName, Seq]
這有可能在華而不實?
謝謝。我不知道可以在查詢級別應用'''''。得到的MappedProjection正是我想要完成的,比我想象的要簡單得多,即不需要ChildParentProjection。另外一個問題是:'q'之前可以很容易地在'sortBy'中使用,但我似乎無法從'MappedProjection'中提取元組或ChildWithParentName –