0

我正在尝试将现有的 PHP 应用程序移动到 grails 中。

我已经根据现有数据库创建了域,并且代码运行良好。

当我需要在我的域中添加一个额外的布尔字段时,就会出现问题。

我收到以下错误。

2014-06-10 16:24:54,146 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - Unsuccessful: alter table entry add expedite tinyint not null

Error |
2014-06-10 16:24:54,163 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - ALTER 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 'expedite' cannot be added to non-empty table 'entry' because it does not satisfy these conditions.

我试图在变量本身中指定默认值。

boolean expedite = false

我还尝试在静态映射中添加默认值,如下所示:

static mapping = {
    table 'entry'
    expedite defaultValue: false
    version false
}

但是错误仍然出现。知道我哪里出错了吗?我正在使用 sql server 2012。

4

3 回答 3

1

由于默认情况下 mysql 将布尔字段映射为一位值,因此布尔字段的值不能为空。

通过以下方式手动更新现有记录:

update my_table set expedite = 0;

或者您可以使用 grails 数据库迁移插件为您生成迁移。

域类中的任何原始数据类型都会获得默认值,因此如果您已经定义了新字段,Boolean expedite那么它可以使用空值。

因此,请始终确保使用原始和非原始数据类型。

于 2014-06-11T04:57:55.717 回答
0

尝试使用类,而不是原始类型:布尔加速

于 2014-06-11T06:40:38.977 回答
0

看起来 sql server 使用 0 和 1 而不是 TRUE/FALSE。这是你想要的?

https://forum.hibernate.org/viewtopic.php?f=1&t=996345

另一个人这样解决...

http://codexplo.wordpress.com/2012/06/21/mapping-a-boolean-with-hibernate/

看起来你只需要在你的域中创建一个方法来拦截和转换。

于 2014-06-11T04:37:25.967 回答