2013-12-15 119 views
6

我已經安裝了個Grails的插件頁面春季安全應用程序運行grails s2-quickstart com.testApplication.secureApplication User Role來自動生成我做grails run-app域對象後,得到了這個異常春季安全域對象:Grails的2 - 無法創建

|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. 

的東西是我的數據庫配置正確,因爲我得到表roleuser_role。但是,用戶不會在我的postgresql db中生成。我對我的自動生成user domain object實現看起來像這樣:

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) 
    } 
} 

我很欣賞你的答案!

回答

8

'user'是Postgres中的保留名稱。您可以通過在域類中設置映射並使用替代名稱來避免此問題。

static mapping = { table 'myusers' } 

這樣你的域類保持不變。您也可以使用與「密碼」相似的方式來避開該名稱。

+0

從來沒有'user'是一個關鍵詞的思路,謝謝! –

0

在用戶域中,添加以下映射配置,以保持域/表名稱user類似於跳過password列的方式。

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

一篇博客文章中解釋的問題和解決方案,可以發現here

+0

試過這個解決方案。它通過了第一個問題,但隨後它會產生一個序列命名的新問題:spi.SqlExceptionHelper錯誤:關係「seq_」user「」不存在 我認爲避免postgres中名爲'user'的表最好。 –

+0

@EdJ我沒有使用任何序列命名,我沒有看到這個問題。正如你所說,如果你可以避免一個名爲'用戶'的表,這是很好,但我喜歡保持這個名字。 –