我在PostgreSql中有一个架构,我想在update set
字段中users_id
执行:
CREATE TABLE if not exists rooms (
oid char(24),
owner_id char(24) not null,
users_id text[],
PRIMARY KEY (oid)
);
执行sql如下:
update rooms set users_id = (select array_agg(distinct e) from
unnest(users_id || '{5a16f7ce77c8a2b22406fb86}') e) where oid =
'5a16f7ce77c8a2b22406fb86';
更新users_id
数组并执行distinct
操作。
在Quill中,我尝试使用该方法:
def addUserInRoom(userId: ObjectId, roomId: ObjectId): Unit = {
val q = quote(
(uid: String, rid: String) =>
infix"""update rooms set users_id = (select array_agg(distinct e) from unnest(users_id || '{${uid}}') e) where oid = '${rid}'""".as[Query[Long]]
)
run(q(lift(userId.toString), lift(roomId.toString)))
}
发生了异常:
Exception in thread "main" org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:65)
at org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:128)
at org.postgresql.jdbc.PgPreparedStatement.bindString(PgPreparedStatement.java:1029)
at org.postgresql.jdbc.PgPreparedStatement.setString(PgPreparedStatement.java:369)
at org.postgresql.jdbc.PgPreparedStatement.setString(PgPreparedStatement.java:353)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setString(HikariProxyPreparedStatement.java)
at io.getquill.context.jdbc.Encoders.$anonfun$stringEncoder$2(Encoders.scala:44)
at io.getquill.context.jdbc.Encoders.$anonfun$stringEncoder$2$adapted(Encoders.scala:44)
...
如何使用scala Quill库执行sql?
不同的方式总是欢迎!
感谢
依赖是:
"org.postgresql" % "postgresql" % "42.1.4",
"io.getquill" %% "quill-jdbc" % "2.2.0",
我的驱动程序实例是:
lazy val ctx = new PostgresJdbcContext(
NamingStrategy(SnakeCase, PostgresEscape),
AppConfig.quill
)
此外,一些简单的sql测试成功。
答案 0 :(得分:1)
{{1}}