我有以下要对Postgresql数据库执行的SQL代码:
sql"""WITH s AS (
SELECT id
FROM $tableName
WHERE $columnName = $value
), i AS (
INSERT INTO $tableName ($columnName)
SELECT $value
WHERE NOT EXISTS (
SELECT id
FROM $tableName
WHERE $columnName = $value
)
RETURNING id
)
SELECT id
FROM i
UNION ALL
SELECT id
FROM s
"""
如果我在update
中使用updateAndRetrieveGeneratedKey
或localTx
,则会得到org.postgresql.util.PSQLException: A result was returned when none was expected.
。如果在single
中使用readOnly
,则会得到类似... cannot perform an insert in a read only query
的信息。
正确的解决方法是什么?