使用 HugSQL 一次将多行插入 PostgreSQL table 与 ON CONFLICT DO UPDATE
Using HugSQL to INSERT multiple rows at once into PostgreSQL table with ON CONFLICT DO UPDATE
我正在使用 PostgreSQL 并希望使用 ON CONFLICT DO UPDATE
语句一次 INSERT
多行。
我有这样的东西:
-- :name add-things! :! :n
INSERT INTO my_table (
p,
foo
)
VALUES :tuple*:values
ON CONFLICT (p) DO UPDATE
SET my_table.foo = foo
其中 p
是主键。
我称之为:
(add-things! {:values [[1 1] [2 3]]})
但是这个returns:
org.postgresql.util.PSQLException: ERROR: column reference "foo" is ambiguous
.
使用SET my_table.foo = :foo
(带关键字参数)导致clojure.lang.ExceptionInfo: Parameter Mismatch: :foo parameter data not found
,因为使用:tuple*:values
语法时没有关键字参数。
知道如何完成这个吗?也许通过在 HugSQL 查询中使用 Clojure 代码?
这里的问题是在冲突解决中只使用了foo
。 "insert data" 中有一个 foo,实际 table 中有一个。您需要以某种方式解决 "insert data" 以解决该冲突。 docs中所述的解决方案是:
conflict_action
specifies an alternative ON CONFLICT
action. It can be either DO NOTHING
, or a DO UPDATE
clause specifying the exact details of the UPDATE
action to be performed in case of a conflict. The SET
and WHERE
clauses in ON CONFLICT DO UPDATE
have access to the existing row using the table's name (or an alias), and to rows proposed for insertion using the special excluded
table.
所以
...
SET foo = excluded.foo
解决冲突。
我正在使用 PostgreSQL 并希望使用 ON CONFLICT DO UPDATE
语句一次 INSERT
多行。
我有这样的东西:
-- :name add-things! :! :n
INSERT INTO my_table (
p,
foo
)
VALUES :tuple*:values
ON CONFLICT (p) DO UPDATE
SET my_table.foo = foo
其中 p
是主键。
我称之为:
(add-things! {:values [[1 1] [2 3]]})
但是这个returns:
org.postgresql.util.PSQLException: ERROR: column reference "foo" is ambiguous
.
使用SET my_table.foo = :foo
(带关键字参数)导致clojure.lang.ExceptionInfo: Parameter Mismatch: :foo parameter data not found
,因为使用:tuple*:values
语法时没有关键字参数。
知道如何完成这个吗?也许通过在 HugSQL 查询中使用 Clojure 代码?
这里的问题是在冲突解决中只使用了foo
。 "insert data" 中有一个 foo,实际 table 中有一个。您需要以某种方式解决 "insert data" 以解决该冲突。 docs中所述的解决方案是:
conflict_action
specifies an alternativeON CONFLICT
action. It can be eitherDO NOTHING
, or aDO UPDATE
clause specifying the exact details of theUPDATE
action to be performed in case of a conflict. TheSET
andWHERE
clauses inON CONFLICT DO UPDATE
have access to the existing row using the table's name (or an alias), and to rows proposed for insertion using the specialexcluded
table.
所以
...
SET foo = excluded.foo
解决冲突。