Scala Slick:如何进行除连接(左外连接为空)?
Scala Slick: How to do an except join (left outer join with is null)?
我正在使用 scala 2.11、slick 2.1.0 和 postgres 9.6。大版本升级是不可能的。
我有 2 个相同的表(从同一模型创建),我想做一个除外连接(左外连接为空):
我的模型是这样的:
trait Coffee {
val coffeeLoversId: Option[Int]
val taxId: Option[Long]
val internationalCoffeeId: String
val providerId: String
}
class Coffees(tag: Tag, schema: String) extends Table[Coffee](tag, Some(schema), "coffees")
val coffeeLoversId: Column[Option[Int]] = column[Option[Int]]("coffee_lovers_id")
val taxId: Column[Option[Long]] = column[Option[Long]]("tax_id")
val internationalCoffeeId: Column[String] = column[String]("international_coffee_id", O.DBType("VARCHAR"))
val providerId: Column[String] = column[String]("provider_id", O.DBType("VARCHAR"))
def * = (coffeeLoversId, taxId, internationalCoffeeId, providerId) <> (Coffee.tupled, Coffee.unapply)
def ? = (coffeeLoversId, taxId, internationalCoffeeId.?, providerId.?).shaped.<>({r=>import r._; _1.map(_=> Coffee.tupled((_1, _2, _3.get, _4.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
}
object Coffees {
def tableQuery(implicit schema: TableSchema) = TableQuery[Coffees]{ tag : Tag => new Coffees(tag, schema.name) }
}
我想要 运行 的 SQL 查询是:
SELECT * from previous.coffees PRV
LEFT JOIN current.coffees CUR
ON PRV.international_coffee_id = CUR.international_coffee_id
WHERE PRV.international_coffee_id IS NULL;
我达到了:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
} yield(c)
这似乎给了我一个左外连接,但我在过滤空值服务器端时遇到了问题(添加 WHERE PRV.international_coffee_id IS NULL
)
如有任何指点或想法,我将不胜感激。
我能够实现我的目标。首先,我尝试使用 scala 的条件理解:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
if(s.internationalCoffeeId.isEmpty)
} yield(c)
这有效,并且在数据库上测试我得到了预期的结果,因为我知道我是在 scala 中而不是在数据库服务器上过滤结果。
所以我一直努力,终于做到了
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on(
(x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
} yield(c)
我还不清楚为什么编译器不能在我的过滤器中正确地获取匿名函数的输入参数的类型,但我通过直接在元组上操作来处理它。
我验证了我正在使用 selectStatement
直接在我的 Query
上获得所需的 SQL 查询(或者在这种情况下我的 CompiledStreamingExecutable
):
val query = Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on(
(x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
query.selectStatement
我正在使用 scala 2.11、slick 2.1.0 和 postgres 9.6。大版本升级是不可能的。
我有 2 个相同的表(从同一模型创建),我想做一个除外连接(左外连接为空):
我的模型是这样的:
trait Coffee {
val coffeeLoversId: Option[Int]
val taxId: Option[Long]
val internationalCoffeeId: String
val providerId: String
}
class Coffees(tag: Tag, schema: String) extends Table[Coffee](tag, Some(schema), "coffees")
val coffeeLoversId: Column[Option[Int]] = column[Option[Int]]("coffee_lovers_id")
val taxId: Column[Option[Long]] = column[Option[Long]]("tax_id")
val internationalCoffeeId: Column[String] = column[String]("international_coffee_id", O.DBType("VARCHAR"))
val providerId: Column[String] = column[String]("provider_id", O.DBType("VARCHAR"))
def * = (coffeeLoversId, taxId, internationalCoffeeId, providerId) <> (Coffee.tupled, Coffee.unapply)
def ? = (coffeeLoversId, taxId, internationalCoffeeId.?, providerId.?).shaped.<>({r=>import r._; _1.map(_=> Coffee.tupled((_1, _2, _3.get, _4.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
}
object Coffees {
def tableQuery(implicit schema: TableSchema) = TableQuery[Coffees]{ tag : Tag => new Coffees(tag, schema.name) }
}
我想要 运行 的 SQL 查询是:
SELECT * from previous.coffees PRV
LEFT JOIN current.coffees CUR
ON PRV.international_coffee_id = CUR.international_coffee_id
WHERE PRV.international_coffee_id IS NULL;
我达到了:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
} yield(c)
这似乎给了我一个左外连接,但我在过滤空值服务器端时遇到了问题(添加 WHERE PRV.international_coffee_id IS NULL
)
如有任何指点或想法,我将不胜感激。
我能够实现我的目标。首先,我尝试使用 scala 的条件理解:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
if(s.internationalCoffeeId.isEmpty)
} yield(c)
这有效,并且在数据库上测试我得到了预期的结果,因为我知道我是在 scala 中而不是在数据库服务器上过滤结果。
所以我一直努力,终于做到了
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on(
(x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
} yield(c)
我还不清楚为什么编译器不能在我的过滤器中正确地获取匿名函数的输入参数的类型,但我通过直接在元组上操作来处理它。
我验证了我正在使用 selectStatement
直接在我的 Query
上获得所需的 SQL 查询(或者在这种情况下我的 CompiledStreamingExecutable
):
val query = Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on(
(x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
query.selectStatement