添加Postgres约束时,"user"处或附近的语法错误

Kev*_*rke 19 postgresql

我正在运行Postgres 8.4.13,并尝试向现有表添加约束.根据文档,这应该是可能的:

alter table indexed_friends add constraint no_duplicate_user_friends unique (user, friend);
Run Code Online (Sandbox Code Playgroud)

然而,当我运行这个时,我收到以下错误:

ERROR:  syntax error at or near "user"
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我几乎完全遵循文档中列出的唯一约束示例.我可以提供表模式,但由于它抱怨语法错误,我不确定是否有必要.

Kev*_*rke 31

啊......这个user词在Postgres中是一个保留词.

用引号括起来:

alter table indexed_friends add constraint no_duplicate_user_friends unique ("user", friend);
Run Code Online (Sandbox Code Playgroud)

工作.

  • 更好的是,不要使用保留字作为列名.这只是一连串的悲伤和这样的问题. (4认同)