【发布时间】:2026-01-26 11:55:02
【问题描述】:
我想在使用slick 3.0 时更新表的前 n 行而不是整行
这是所有版本的更新:
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)
【问题讨论】:
-
Lesson -> 如果你不能用 SQL 做某事,那么你不能用 Slick 做那件事。
-
@SarveshKumarSingh 哎呀,你可以用 SQL 轻松做到这一点。你为什么这么说?