Cannot create a database table named 'user' in PostgreSQL

穿精又带淫゛_ 提交于 2019-11-26 04:52:20

问题


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

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


回答1:


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

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

create table "user" (...);

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

If you want to save yourself a lot of trouble use a different name. 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




回答2:


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

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



回答3:


We had this same issue time ago, and we just changed the table name from user to 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.



来源:https://stackoverflow.com/questions/22256124/cannot-create-a-database-table-named-user-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!