简体   繁体   English

Grails:休眠和数据迁移

[英]Grails: Hibernate and Data migrations

I have a domain class that is for a customer. 我有一个针对客户的域类。 I have had to change to domain class recently because we were gathering a billing address and a shipping address, but were only recording one zip code value. 我最近不得不更改为域类,因为我们正在收集账单地址和收货地址,但只记录一个邮政编码值。 So I thought what a simple fix. 所以我认为这是一个简单的解决方法。 Change zipcode to billzipcode and then create a new field for shipzipcode. 将邮政编码更改为billzipcode,然后为shipzipcode创建一个新字段。 I ran dbm-gorm-diff to create the change log and then ran dbm-update to execute the changes I've made. 我运行dbm-gorm-diff来创建更改日志,然后运行dbm-update来执行我所做的更改。 Everything to this point works swimmingly. 到目前为止,一切都可以顺利进行。 But then I go to debug the app to see that the changes to datasource were ok. 但是然后我去调试该应用程序,以查看对数据源的更改是可以的。 However, now when I try to save a new customer record it get this error: 但是,现在当我尝试保存新的客户记录时,出现此错误:

    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)

I'm at a loss because the ZIPCODE column is not longer in the domain class. 我很茫然,因为ZIPCODE列不再在域类中。 Any guidance would be appreciated. 任何指导将不胜感激。

EDIT: per request here are the changes that were generated by the migrations plugin: 编辑:每个请求在这里是由迁移插件生成的更改:

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")
}

If you notice your changeset the entry for billzipcode is actually adding a new column instead of renaming the zipcode column. 如果您注意到更改集,那么billzipcode的条目实际上是在添加一个新列,而不是重命名zipcode列。 Because of this you would still have to manage the data migration. 因此,您仍然必须管理数据迁移。 Something like this should work in you case: 这样的事情应该在您的情况下工作:

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 a related question and @Burt's accepted answer points to this link. 这是一个相关的问题,@ Burt接受的答案指向链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM