gpt4 book ai didi

Scala Slick 3.1 多重连接

转载 作者:行者123 更新时间:2023-12-03 17:06:48 25 4
gpt4 key购买 nike

我正在尝试在 Slick 3.1.1 中执行多个连接。我想达到的结果是这样的:

SELECT * FROM customer LEFT JOIN customer_address ON customer.id = customer_address.customer_id LEFT JOIN address ON customer_address.address_id = address.id

我尝试了以下方法:
val query = for {
c <- Customer
ca <- CustomerAddress if ca.customerId === c.id
a <- Address if a.id === ca.addressId
} yield (c, a)

这里的问题是,如果客户没有地址,它就不会出现,这是有道理的。

然后我尝试了这个:
    val query2 = for {
(c, ca, a) <- (Customer joinLeft CustomerAddress on (_.id === _.customerId)) joinLeft Address on (_._2.addressId === _.id)
} yield (c, a)

这里的问题是我在 _._2.addressId 上遇到错误因为 _._2Rep目的。

确切的错误:
Error:(21, 110) value addressId is not a member of slick.lifted.Rep[Option[Models.Tables.CustomerAddress]]
(c, ca, a) <- (Customer joinLeft CustomerAddress on (_.id === _.customerId)) joinLeft Address on (_._2.addressId === _.id)

自动生成的 Slick Tables 类:
http://pastebin.com/e4M3cGU8

如何使用 Slick 获得我想要的结果?

最佳答案

您还需要做的是映射结果。

val query2 = for {
((c, ca), a) <- (Customer joinLeft CustomerAddress on (_.id === _.customerId)) joinLeft Address on (_._2.map(_.addressId) === _.id)
} yield (c, a)

关于Scala Slick 3.1 多重连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420434/

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