postgres syntax error at or near “ON”

一世执手 提交于 2019-11-29 11:01:13
JohnLBevan

Supported Version

Per @klin's comment above, ON CONFLICT is only supported from PostgreSQL 9.5 onwards.

If you're on an earlier version, there's some great info in this answer: https://stackoverflow.com/a/17267423/361842

Unique Constraint

Add a unique index on activity_name. At present there's no constraints on that column, so there's no possibility for a conflict on that column.

CREATE UNIQUE INDEX UK_config_activity_log__activity_name 
ON config_activity_log (activity_name);

If, however, you don't want that column to be unique, what conflict are you envisaging / what's the issue you're hoping to resolve with the on conflict action?

See conflict_target in https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT


An alternative syntax is to modify your create statement to include the unique condition there; e.g.

CREATE TABLE IF NOT EXISTS config_activity_log
(
  id                      serial primary key,
  activity_name           varchar(100) NOT NULL UNIQUE,
  last_config_version     varchar(50) NOT NULL,
  activity_status         varchar(100) NOT NULL DEFAULT 'Awaiting for cofman',
  cofman_last_update      bigint NOT NULL DEFAULT -1,
  is_error                boolean DEFAULT FALSE,
  activity_timestamp      timestamp DEFAULT current_timestamp
);

According to the error code, your version does not support ON CONFLICT.
On PostreSQL 9.6 the error message is -

[Code: 0, SQL State: 42P10]  ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!