【发布时间】:2019-01-28 02:51:08
【问题描述】:
我正在尝试通过 asyncpg API 使用查询参数在 postgresql 数据库的表中插入值。 我这样创建了我的表:
CREATE TABLE my_table
(
cat BIGINT,
roles BIGINT ARRAY
);
我已经尝试像这样直接在 BIGINT 中转换参数:$1:BIGINT 但我得到了同样的错误
await connection.execute('''
INSERT INTO my_table(cat, roles)
VALUES($1, $2)
ON CONFLICT ($1)
DO UPDATE SET roles = array_append(roles, $2)
''', cat, roles)
cat 是一个 int,角色是一个 int 数组
应该在 my_table 中插入 cat 和角色,但我得到了错误:syntax error at or near "$1"
我提供数据库的日志以防万一
2019-01-26 21:01:22 UTC:172.31.36.115(37598):Barbote@Barbotedb:[15082]:ERROR: syntax error at or near "$1" at character 111
2019-01-26 21:01:22 UTC:172.31.36.115(37598):Barbote@Barbotedb:[15082]:STATEMENT:
INSERT INTO "429792212016955423"(cat, roles)
VALUES($1 $2)
ON CONFLICT ($1)
DO UPDATE SET roles = array_append(roles, $2);
【问题讨论】:
-
看来你的 await connection.execute 行有错字(execute 拼写为 exceute。)我也认为你会想要 ON CONFLICT (cat) 因为这与位置参数无关你赋予价值。
-
我在复制代码时犯了一个错误。我更改了 ON CONFLICT 部分,现在出现了一个新错误
column reference "roles" is ambigious -
roles可以包含重复项吗? -
我只需要这样写
my_table.roles我现在又遇到了另一个错误there is no unique or exclusion constraint matching the ON CONFLICT specification我应该编辑我的帖子还是创建一个新帖子?
标签: python postgresql asyncpg