Search code examples
hibernategrailsgrails-ormgrails-2.0

How can I create a table with name 'user'


I am working in GORM now, I got very complicated issue.

My domain class name is User, to map automatically by GORM it the table name should be user, but it is not allowing to create table with name as 'user'. Because user is a keyword in most of all database systems (I am using PostgresSQL).

I read GORM document from Grails, but I am not able find, how to map table and domain classes it there is any violation in naming conventions.

Or is there a better solution to solve this?

create table user (id varchar(20))


ERROR:  syntax error at or near "user"
LINE 1: create table user (id varchar(20))
                     ^
********** Error **********

ERROR: syntax error at or near "user"
SQL state: 42601
Character: 14

Solution

  • The best solution is to avoid using reserved names such as user.

    However, if you can't then you can can quote the table name in the mapping block with backticks. Then, Hibernate will convert those to whatever the quoting/escaping approach is for the database. So for example:

    static mapping = {
        table "`user`"
    }
    

    Hope this helps.