"重复输入''关键"在使用hasOne的Grails数据库迁移中

时间:2015-01-20 14:11:42

标签: mysql grails grails-2.0 database-migration

使用Grails Database Migration Plugin 1.4我想拥有现有数据库的以下结构:

class StickerGroup {
}

现在我添加了类公司并更新了StickerGroup类:

class StickerGroup {

   static constraints = { company nullable: true, unique: true }
   static belongsTo = [company: Company]
}

class Company {
  static hasOne = [stickerGroup: StickerGroup]
}

我创建了迁移文件,然后执行了grails dbm-update。之后我得到以下错误日志:

2015-01-20 15:03:49,549 [main] ERROR liquibase  - Change Set 2015-01-20-update-Company.groovy::1421762562430-2::mg (generated) failed.  Error: Error executing SQL ALTER TABLE `sticker_group` ADD `company_id` VARCHAR(10) NOT NULL UNIQUE: Duplicate entry '' for key 'company_id'
Message: Error executing SQL ALTER TABLE `sticker_group` ADD `company_id` VARCHAR(10) NOT NULL UNIQUE: Duplicate entry '' for key 'company_id'
    Line | Method
->>   62 | execute           in liquibase.executor.jvm.JdbcExecutor
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    104 | execute           in     ''
|   1091 | execute . . . . . in liquibase.database.AbstractDatabase
|   1075 | executeStatements in     ''
|    317 | execute . . . . . in liquibase.changelog.ChangeSet
|     27 | visit             in liquibase.changelog.visitor.UpdateVisitor
|     58 | run . . . . . . . in liquibase.changelog.ChangeLogIterator
|    114 | update            in liquibase.Liquibase
|     26 | doCall . . . . .  in DbmUpdate$_run_closure1_closure2
|     59 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2_closure11
|    133 | executeInSession  in grails.plugin.databasemigration.MigrationUtils
|     51 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2
^     25 | doCall . . . . .  in DbmUpdate$_run_closure1
Caused by MySQLIntegrityConstraintViolationException: Duplicate entry '' for key 'company_id'
->>  377 | handleNewInstance in com.mysql.jdbc.Util
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    360 | getInstance       in     ''
|    971 | createSQLException in com.mysql.jdbc.SQLError
|   3887 | checkErrorPacket  in com.mysql.jdbc.MysqlIO
|   3823 | checkErrorPacket  in     ''
|   2435 | sendCommand       in     ''
|   2582 | sqlQueryDirect .  in     ''
|   2526 | execSQL           in com.mysql.jdbc.ConnectionImpl
|   2484 | execSQL . . . . . in     ''
|    848 | execute           in com.mysql.jdbc.StatementImpl
|    742 | execute . . . . . in     ''
|     92 | doInStatement     in liquibase.executor.jvm.JdbcExecutor$1ExecuteStatementCallback
|     55 | execute . . . . . in liquibase.executor.jvm.JdbcExecutor
|    104 | execute           in     ''
|   1091 | execute . . . . . in liquibase.database.AbstractDatabase
|   1075 | executeStatements in     ''
|    317 | execute . . . . . in liquibase.changelog.ChangeSet
|     27 | visit             in liquibase.changelog.visitor.UpdateVisitor
|     58 | run . . . . . . . in liquibase.changelog.ChangeLogIterator
|    114 | update            in liquibase.Liquibase
|     26 | doCall . . . . .  in DbmUpdate$_run_closure1_closure2
|     59 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2_closure11
|    133 | executeInSession  in grails.plugin.databasemigration.MigrationUtils
|     51 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2
^     25 | doCall . . . . .  in DbmUpdate$_run_closure1
liquibase.exception.MigrationFailedException: Migration failed for change set 2015-01-20-update-Company.groovy::1421762562430-2::mg (generated):
     Reason: liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE `sticker_group` ADD `company_id` VARCHAR(10) NOT NULL UNIQUE: Duplicate entry '' for key 'company_id':
          Caused By: Error executing SQL ALTER TABLE `sticker_group` ADD `company_id` VARCHAR(10) NOT NULL UNIQUE: Duplicate entry '' for key 'company_id':
          Caused By: Duplicate entry '' for key 'company_id'
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:347)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:27)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:58)
    at liquibase.Liquibase.update(Liquibase.java:114)
    at DbmUpdate$_run_closure1_closure2.doCall(DbmUpdate:26)
    at _DatabaseMigrationCommon_groovy$_run_closure2_closure11.doCall(_DatabaseMigrationCommon_groovy:59)
    at grails.plugin.databasemigration.MigrationUtils.executeInSession(MigrationUtils.groovy:133)
    at _DatabaseMigrationCommon_groovy$_run_closure2.doCall(_DatabaseMigrationCommon_groovy:51)
    at DbmUpdate$_run_closure1.doCall(DbmUpdate:25)
Caused by: liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE `sticker_group` ADD `company_id` VARCHAR(10) NOT NULL UNIQUE: Duplicate entry '' for key 'company_id'
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:62)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:104)
    at liquibase.database.AbstractDatabase.execute(AbstractDatabase.java:1091)
    at liquibase.database.AbstractDatabase.executeStatements(AbstractDatabase.java:1075)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:317)
    ... 8 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '' for key 'company_id'
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:971)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:848)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:742)
    at liquibase.executor.jvm.JdbcExecutor$1ExecuteStatementCallback.doInStatement(JdbcExecutor.java:92)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
    ... 12 more

如何修复此错误?

0 个答案:

没有答案