无法在 PostgreSQL 中创建名为“user”的数据库表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22256124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:38:17  来源:igfitidea点击:

Cannot create a database table named 'user' in PostgreSQL

postgresqlhibernateidentifierreserved-words

提问by Channa

It seems PostgreSQL does not allow to create a database table named 'user'. But MySQL will allow to create such a table.

PostgreSQL 似乎不允许创建名为“用户”的数据库表。但是 MySQL 将允许创建这样的表。

Is that because it is a key word? But Hibernate cannot identify any issue (even if we set the PostgreSQLDialect).

是因为它是一个关键词吗?但是 Hibernate 无法识别任何问题(即使我们设置了 PostgreSQLDialect)。

回答by a_horse_with_no_name

useris a reserved word and it's usually not a good idea use reserved words for identifiers (tables, columns).

user是一个保留字,使用保留字作为标识符(表、列)通常不是一个好主意。

If you insist on doing that you have to put the table name in double quotes:

如果您坚持这样做,则必须将表名放在双引号中:

create table "user" (...);

But then you alwaysneed to use double quotes when referencing the table. Additionally the table name is then case-sensitive. "user"is a different table name than "User".

但是在引用表时你总是需要使用双引号。此外,表名是区分大小写的。"user"是与 不同的表名"User"

If you want to save yourself a lot of trouble use a different name. users, user_account, ...

如果您想省去很多麻烦,请使用不同的名称。users, user_account, ...

More details on quoted identifiers can be found in the manual: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

可以在手册中找到有关引用标识符的更多详细信息:http: //www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

回答by mnv

It is possible to specify tablename with JPA with next syntax:

可以使用以下语法使用 JPA 指定表名:

@Table(name="\"user\"")

回答by Sergio A.

We had this same issue time ago, and we just changed the table name from userto app_user. Due to the use of Hibernate/JPA. We thought it would be easier this way. Hope this little fix will help someone else.

我们之前遇到过同样的问题,我们只是将表名从 更改userapp_user. 由于使用了 Hibernate/JPA。我们认为这样会更容易。希望这个小修复能帮助别人。

回答by AlexElin

One can create table userin the schema other than public. The example:

可以user在架构中创建表而不是public. 这个例子:

CREATE SCHEMA my_schema;
CREATE TABLE my_schema.user(...);