【发布时间】:2014-02-28 01:39:04
【问题描述】:
我有一个 Grails 域类,它是 SQL Server 上的相关数据库表。我想将整数类型的新属性/列 jPId 添加到现有域/表中。这是我的代码:
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 column: "JP_ID", defaultValue: 0?或您希望名称不是“JP_ID”的任何名称。 -
我也尝试过使用专栏,但没有成功。令人讨厌的是,Grails 试图使用命令
alter table EF_JobPremium add jpid int not null更改表......尽管我提到了 nullable:true。 -
这是一个 Hibernate 的东西 - 它可以保护你免受自己的伤害 - 请参阅我的答案
标签: grails grails-orm