hibernate - Grails: hibernate 和数据迁移

标签 hibernate grails gorm data-migration

我有一个针对客户的域类。我最近不得不更改为域类,因为我们正在收集帐单邮寄地址和收货地址,但只记录一个邮政编码值。所以我认为这是一个简单的解决方法。将邮政编码更改为billzipcode,然后为shipzipcode创建一个新字段。我运行dbm-gorm-diff来创建更改日志,然后运行dbm-update来执行我所做的更改。到目前为止,一切都可以顺利进行。但是然后我去调试该应用程序,以查看对数据源的更改是可以的。但是,现在当我尝试保存新的客户记录时,出现此错误:

    NULL not allowed for column "ZIPCODE"; SQL statement:
insert into customer (id, version, billaddr, billcity, billstate, billzipcode, cell, contact, country_id, custcode, custname, date_created, email, fax, last_updated, organization, phone, shipaddr, shipasbill, shipcity, shipstate, shipzipcode, status, tenant_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-164]. Stacktrace follows:
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ZIPCODE"; SQL statement:
insert into customer (id, version, billaddr, billcity, billstate, billzipcode, cell, contact, country_id, custcode, custname, date_created, email, fax, last_updated, organization, phone, shipaddr, shipasbill, shipcity, shipstate, shipzipcode, status, tenant_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-164]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.table.Column.validateConvertUpdateSequence(Column.java:293)
    at org.h2.table.Table.validateConvertUpdateSequence(Table.java:680)
    at org.h2.command.dml.Insert.insertRows(Insert.java:120)
    at org.h2.command.dml.Insert.update(Insert.java:84)
    at org.h2.command.CommandContainer.update(CommandContainer.java:73)
    at org.h2.command.Command.executeUpdate(Command.java:226)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:143)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:129)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at com.companyName.appname.billing.CustomerController$_closure6.doCall(CustomerController.groovy:45)
    at grails.plugin.multitenant.core.servlet.CurrentTenantServletFilter.doFilter(CurrentTenantServletFilter.java:53)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

我很茫然,因为ZIPCODE列不再在域类中。任何指导将不胜感激。

编辑:
每个请求都是迁移插件生成的更改:
changeSet(author: "user (generated)", id: "1363806498118-1") {
    dropColumn(tableName: "admission", columnName: "pen")
    addColumn(tableName: "admission") {
        column(name:"pen_id", type:"bigint")
    }       
}


changeSet(author: "user (generated)", id: "1363806498118-2") {
    addColumn(tableName: "customer") {
        column(name: "billzipcode", type: "varchar(50)") {
            constraints(nullable: "false")
        }
    }
}

changeSet(author: "user (generated)", id: "1363806498118-3") {
    addColumn(tableName: "customer") {
        column(name: "shipzipcode", type: "varchar(50)") {
            constraints(nullable: "false")
        }
    }
}

changeSet(author: "user (generated)", id: "1363806498118-4") {
    addColumn(tableName: "customer_costs") {
        column(name: "subtotal", type: "float(19)") {
            constraints(nullable: "false")
        }
    }
}


changeSet(author: "user (generated)", id: "1363806498118-5") {
    addForeignKeyConstraint(baseColumnNames: "pen_id", baseTableName: "admission", constraintName: "FK1A21809D0C6EF15", deferrable: "false", initiallyDeferred: "false", referencedColumnNames: "id", referencedTableName: "pen", referencesUniqueColumn: "false")
}

changeSet(author: "user (generated)", id: "1363806498118-6") {
    dropColumn(columnName: "zipcode", tableName: "customer")
}

最佳答案

如果您注意到更改集,那么billzipcode的条目实际上是在添加新列,而不是重命名zipcode列。因此,您仍然必须管理数据迁移。这样的事情应该在您的情况下工作:

changeSet(author: 'user (generated)', id: '1363806498118-4') {
    comment { 'Renaming zipcode to billzipcode' }
    renameColumn(tableName: 'customer_costs', oldColumnName: 'zipcode', newColumnName: 'billzipcode', columnDataType: 'varchar(50)')
}

Here's相关问题,@ Burt接受的答案指向this链接。

关于hibernate - Grails: hibernate 和数据迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556220/

相关文章:

java - 在 Restful Web 服务中输入数据验证(是否使用空对象)

java - JPA、Mysql Blob 返回数据过长

java - 使用 hibernate 以两个列表作为参数从数据库获取对象

Grails:禁用 GSP 中的强制字段验证

html - Grails 2.5无法在js文件中找到图像路径

grails - 展平嵌套的Grails Domain对象

grails - 用grails控制台创建的新域对象在dbconsole中不可见

hibernate - Java Hibernate with Persistence Question---如果没有定义FetchType,默认的方法是什么?

grails - 数据库功能查询到Gorm字段的结果

hibernate - HQL和hasMany属性