Scala 如何将一个光滑查询的结果用于另一个中间有计算的查询
我有一个数据库表InventoryLotUsage,其中包含id、inventoryLotId和date列。我想删除一个InventoryLot,但在此之前,我需要根据日期和一些其他条件更新具有外键inventoryLotId的InventoryLotUsage行。Scala 如何将一个光滑查询的结果用于另一个中间有计算的查询,scala,slick,Scala,Slick,我有一个数据库表InventoryLotUsage,其中包含id、inventoryLotId和date列。我想删除一个InventoryLot,但在此之前,我需要根据日期和一些其他条件更新具有外键inventoryLotId的InventoryLotUsage行。 我的问题是如何使用一元联接查询数据,对其进行一些计算,并使用这些计算的结果在Slick中运行一个更新所有事务 我试图得到一系列这样的行 for { il <- InventoryLot.filter(_.id ==
我的问题是如何使用一元联接查询数据,对其进行一些计算,并使用这些计算的结果在Slick中运行一个更新所有事务 我试图得到一系列这样的行
for {
il <- InventoryLot.filter(_.id === id)
lotUsage <- InventoryLotUsage.filter(_.inventoryLotId === id).result
groupedUsage = lotUsage.groupBy(_.date)
...
}
用于{
il您需要撰写以归档所需结果。例如:
加载所需的所有数据
val loadStaffAction = (for {
il <- InventoryLot.filter(_.id === id)
lotUsage <- InventoryLotUsage.filter(_.inventoryLotId === id)
} yield (il, lotUsage)).result
在此之后,您可以在一个事务中运行所有查询
db.run(updateAction.transactionally)
我想你忘了在il上的结果了
val updateAction = loadStaffAction.map { result =>
// creating update statements based on some computations and conditions
DBIO.seq(
InventoryLotUsage.filter(_.id === inventory1.id).map(_.name).update("new value"),
InventoryLotUsage.filter(_.id === inventory2.id).map(_.name).update("another value"),
);
}
db.run(updateAction.transactionally)