系统和本地数据库中的 Postgres 重叠 Table 名称

Postgres Overlap in System and Local DB Table Name

我 运行 遇到了一个 st运行ge 问题,我的本地数据库中有一个 user table,它似乎与 postgres 系统重叠 table user 当我尝试使用 Postico 运行 SQL 查询时。当我 运行ning 查询我的数据库时,我返回一个映射到访问数据库的用户名的 current_user 列。我应该如何更改我的 SQL 以指向本地数据库用户 table?

SQL 我正在尝试的命令 运行:

SELECT *
FROM user
;

返回current_user: johndoe

UPDATE user
SET user.password = 'cryptopw', user.authentication_token = NULL
WHERE user.user_id = 210;

USER 保留 key words for PostgreSQL 之一。它也保留给 SQL 2011、2008 和 92。

来自文档:

SQL distinguishes between reserved and non-reserved key words. According to the standard, reserved key words are the only real key words; they are never allowed as identifiers.

强调我的

也就是说,如果你想避免麻烦,不要使用user作为标识符(即:它不应该是一个table或列的名称,或索引,或约束,或函数,...)。

如果你还想尝试:

As a general rule, if you get spurious parser errors for commands that contain any of the listed key words as an identifier you should try to quote the identifier to see if the problem goes away.

即试:

SELECT
    *
FROM
    "user" ;

UPDATE 
    "user"
SET 
    password = 'cryptopw',        -- not "user".password
    authentication_token = NULL   -- not "user". authentication_token
WHERE 
    "user".user_id = 210;         -- you can, but don't need "user"

我实际上会重命名 table,将其命名为 my_user(或 application_user 或其他名称),遵循 SQL 标准,而无需引用标识符。