2014-12-05 51 views
2

我正在使用grails 2.4.4。grails派生的屬性在數據庫中創建列

我有域

class Post { 
    Integer nbrOfFavorites 
    static hasMany = [ 
     favorites : Favorite 
    ] 

    static mappings = { 
     nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))' 
    } 
} 

的問題是,在數據庫中創建nbrOfFavorites,所以檢索它並沒有考慮到的公式。

我的語法有什麼問題嗎?

感謝

+0

有你把你的數據庫,並添加公式項後,再次開始了嗎?如果您在添加「公式」選項之前運行應用程序,那麼架構可能已經將該變量存儲在該表中了? – nickdos 2014-12-06 02:26:03

+0

我會在我的開發環境中嘗試此操作,但是我無法在我的生產環境中執行此操作,有沒有其他方法? – 2014-12-06 06:05:53

+0

數據庫遷移插件應該處理它 - http://grails.org/plugin/database-migration ...如果這是問題。 – nickdos 2014-12-06 07:59:19

回答

3

是存在的語法拼寫錯誤。更改mappingsmapping,如:

static mapping = { 
    nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))' 
} 

參考#Grails Domain: mapping

+0

就是這樣!我不明白刪除級聯在我的項目中如何處理這個錯字:|。非常感謝 – 2014-12-12 11:06:50

0

嘗試添加該字段爲一過性的域對象:

class Post { 
Integer nbrOfFavorites 
static hasMany = [ 
    favorites : Favorite 
] 

static mappings = { 
    nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))' 
} 

static transients = ['nbrOfFavorites'] 
} 
+0

我已經試過這個,它不起作用。 – 2014-12-08 09:50:33