Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails 2 - Cannot create spring security domain object

I have installed the spring security app from th grails plugin page after running grails s2-quickstart com.testApplication.secureApplication User Role to autogenerate the domain object I did grails run-app and got this exception:

|Loading Grails 2.3.4
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
Precompiling AST Transformations ...
src C:\Users\GrailsWorkspace\testApplication\target\work\plugins\postgresql-extensions-0.6.1 C:\Users\GrailsWorkspace\testApplication\target\classes
Done precompiling AST Transformations!
..
|Compiling 3 source files
...................................................
|Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Error |
2013-12-15 15:42:45,635 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - Unsuccessful: create table user (id int8 not null, version int8 not null, account_expired bool not null, account_locked bool not null, enabled bool not null, "password" varchar(255) not null, password_expired bool not null, username varchar(255) not null unique, primary key (id))
Error |
2013-12-15 15:42:45,638 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - ERROR: syntax error at "user"
  Position: 14
Error |
2013-12-15 15:42:45,688 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - Unsuccessful: alter table user_role add constraint FK143BF46A1E03E05D foreign key (user_id) references user
Error |
2013-12-15 15:42:45,688 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - ERROR: syntax error at "user"
  Position: 90
|Server running. 

The thing is my database is correctly configured, because I get the table role and user_role. However user does not get generate in my postgresql db. My implementation of my autogenerated user domain object looks like that:

class User {

    transient springSecurityService

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static transients = ['springSecurityService']

    static constraints = {
        username blank: false, unique: true
        password blank: false
    }

    static mapping = {
        password column: '`password`'
    }

    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

I appreciate your answer!

like image 489
Carol.Kar Avatar asked Dec 15 '13 14:12

Carol.Kar


1 Answers

'user' is a reserved name in Postgres. You can avoid this by setting up a mapping in your domain class and use an alternate name.

 static mapping = { table 'myusers' }

This way your domain class remains the same. You could also escape the name similar to the way you have done with 'password'.

like image 109
Joshua Moore Avatar answered Sep 24 '22 09:09

Joshua Moore