gpt4 book ai didi

scala - 光滑地联接两个表并获得两个表的结果

转载 作者:行者123 更新时间:2023-12-04 04:34:28 24 4
gpt4 key购买 nike

我有一个像这样的多对多关系设置:

人员<->人员字段<->字段

现在,我不仅要查询Person的所有字段(我可以做到),而且要查询PersonField与Person的Field的联接版本。 (我也想查询/检索数据透视表/中间表“PersonField”中的信息!)

人:

case class Person(id: Long, name: String)
{
def fields =
{
person <- Persons.all.filter(_.id === this.id)
field <- person.fields
} yield field
}

class Persons(tag: Tag) extends Table[Person](tag, "persons")
{
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")

def * = (id, name) <> (Person.tupled, Person.unapply)
def fields = PersonFields.all.filter(_.personID === id).flatMap(_.fieldFK)
}

object Persons
{
lazy val all = TableQuery[Persons]
}

field :
case class Field(id: Long, name: String, description: Option[String])

class Fields(tag: Tag) extends Table[Field](tag, "fields")
{
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def description = column[Option[String]]("description")

def * = (id, name, description) <> (Field.tupled, Field.unapply)
}

object Fields
{
lazy val all = TableQuery[Fields]
}

人员字段:
case class PersonField(id: Long, personID: Long, fieldID: Long, value: String)

// TODO add constraint to make (personID, fieldID) unique
class PersonFields(tag: Tag) extends Table[PersonField](tag, "person_field")
{
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def personID = column[Long]("person_id")
def fieldID = column[Long]("field_id")
def value = column[String]("value")

def * = (id, personID, fieldID, value) <> (PersonField.tupled, PersonField.unapply)

def personFK = foreignKey("person_fk", personID, Persons.all)(_.id)
def fieldFK = foreignKey("field_fk", fieldID, Fields.all)(_.id)
}

object PersonFields
{
lazy val all = TableQuery[PersonFields]
}

现在要查询Person的所有字段,我有一个小助手类:
def getFields(p: Person): Future[Seq[Field]] =
{
val query = p.fields
db.run(query.result)
}

所以我可以做

val personX ...
personX.onSuccess
{
case p: Person =>
{
val fields = helper.getFields(p)
fields.onSuccess
{
case f: Seq[Field] => f foreach println
}
}
}

现在,personX的每个字段都被打印到控制台。奇迹般有效。

事情是,我也想获得PersonField(与Field一起使用)!

因此,我尝试了以下更改(在其他无效的操作中,我不记得了)

中的人中:
def fields =
{
for
{
person <- Persons.all.filter(_.id === this.id)
field <- person.fields join Fields.all on (_.fieldID === _.id)
} yield field
}

中的人
def fields = PersonFields.all.filter(_.personID === id)//这里没有flatMap!

然后getFields(p:Person)看起来像这样:
def getFields(p: Person): Future[Seq[(PersonField, Field)]]


personX.onSuccess
{
case p: Person =>
{
val fields = helper.getFields(p)
fields.onSuccess
{
case f: Seq[(PersonField, Field)] => f map(f => println(f._1)}
}
}
}

没有给我任何东西,所以我想我的加入一定是错误的。但是我到底在做什么错呢?

最佳答案

您可以将全部三个都加入,然后得出结果

for {
((personField, person), field) <- PersonFields.all join Persons.all on (_.personId === _.id) join Fields.all on (_._1.fieldId === _.id)
if person.id === this.id
} yield (personField, person, field)

(我不确定我确切地知道了您要从查询中获取什么,因此您只需编辑yield即可)

关于scala - 光滑地联接两个表并获得两个表的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32468114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com