我想在使用slick 3.0
这是所有版本的更新:
private[this] val active = this.filter(a => a.status =!= AccountStatus.DISABLED)
db.run(
active.filter(a => a.usedBy.isEmpty || a.usedBy === Host.name)
.map(account => account.usedBy)
.update("host-a")
)
我尝试使用此版本,但它没有工作并抛出异常
private[this] val active = this.filter(a => a.status =!= AccountStatus.DISABLED)
db.run(
active.filter(a => a.usedBy.isEmpty || a.usedBy === Host.name)
.take(10)
.map(account => account.usedBy)
.update(Option(Host.name))
)
异常
Caused by: slick.SlickException: A query for an UPDATE statement must resolve to a comprehension with a single table -- Unsupported shape: Comprehension s2, Some(Apply Function and), None, ConstArray(), None, None, Some(LiteralNode 100 (volatileHint=false)), None
at slick.driver.JdbcStatementBuilderComponent$QueryBuilder.buildUpdate(JdbcStatementBuilderComponent.scala:447)
at slick.driver.JdbcProfile$$anonfun$updateCompiler$1.apply(JdbcProfile.scala:30)
at slick.driver.JdbcProfile$$anonfun$updateCompiler$1.apply(JdbcProfile.scala:30)
at slick.jdbc.JdbcMappingCompilerComponent$JdbcCodeGen.compileServerSideAndMapping(JdbcMappingCompilerComponent.scala:59)
答案 0 :(得分:0)
嗯......对此的答案在于你正试图做一些Slick不应该做的事情。
一个非常简单的指南 - 如果有疑问,请在SQL中思考然后转换为Slick
想想你将如何在SQL中实现这一目标,
如果我转换您的"查询",
// lets say Host.name = "Awesome-Host"
active.filter(a => a.usedBy.isEmpty || a.usedBy === Host.name)
.take(10)
.map(account => account.usedBy)
.update(Option(Host.name))
到SQL,它将是这样的,
UPDATE
active
SET
used_by = 'Awesome-Host'
WHERE
used_by IS NULL
OR used_by = 'Awesome-Host'
LIMIT 10
从SQL的角度来看,这有点荒谬......
现在......让我们谈谈你将如何用SQL实现它,
UPDATE
(
SELECT
*
FROM
active
WHERE
used_by IS NULL
OR used_by = 'Awesome-Host'
LIMIT 10
) active_selection
SET
active_selection.used_by = 'Awesome-Host'
并且......可以使用子查询将其转换为Slick
val activeSelection = active
.filter(a => a.usedBy.isEmpty || a.usedBy === Host.name)
.take(10)
val updateSelection = activeSelection
.map(a => a.usedBy)
.update(Option(Host.name))