我有一個Grails域類,它是SQL Server上的相關數據庫表。我想添加一個新的屬性/列jPId類型的整數到現有的域/表。這裏是我的代碼有:Grails:無法將整數屬性添加到域類
class JobCode{
int jPId // This is the new property/column I want to add.
//Other Properties which already have corresponding columns in DB
static mapping = {
jPId defaultValue: 0
}
static constraints = {
jPId nullable:true // tried commenting this line as well
//Other properties
}
}
我的DataSource配置文件有DB模式設置爲「更新」,這樣,我可以將執行任何ALTER操作。現在,當我重新啓動服務器時,我期望它在數據庫中創建新的整數/數字列。但它會拋出下面的錯誤,並不能創建列:
ERRORorg.hibernate.tool.hbm2ddl.SchemaUpdateUnsuccessful: alter table EF_JobCode add jpid int not null
ERRORorg.hibernate.tool.hbm2ddl.SchemaUpdateALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'jpid' cannot be added to non-empty table 'EF_JobCode' because it does not satisfy these conditions.
我很無知,因爲我不知道我錯過了什麼!我在上面的代碼中給了int jPId一個默認值,並且也將它聲明爲nullable:true(也對它進行了評論,因爲我不確定nullable:true是否適用於基本類型)。沒有任何工作,我不斷看到上述錯誤。
您可以在映射中指定想要的實際列名並嘗試嗎? 'jPId列:「JP_ID」,defaultValue:0'?或者你想要的名稱不是「JP_ID」。 – dmahapatro
我只是嘗試了列,但徒勞無功。令人討厭的是,Grails試圖使用命令「alter table EF_JobPremium add jpid int not null」來改變表格......儘管我提到了可空:true。 – rajugaadu
這是一個休眠的東西 - 它保護你自己 - 看到我的回答 –