我在迁移中有以下变更集:
<changeSet author="aaa (generated)" id="1416295794385-38" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<addForeignKeyConstraint baseColumnNames="fk_xxx_id" baseTableName="client_xxxs" constraintName="FK_2kma3uvjxartf5p9l24yk2fxu" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="xxx_types"/>
</changeSet>
在执行此操作时出现错误:
liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`yyy_test`.`client_xxxs`, CONSTRAINT `FK_2kma3uvjxartf5p9l24yk2fxu` FOREIGN KEY (`fk_xxx_id`) REFERENCES `xxx_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
但是当我在mysql中检查架构时,似乎会应用此约束。这是我应用约束的唯一时间,并且之前没有应用过。
show create table client_xxxs;
| client_xxxs | CREATE TABLE `client_xxxs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
`fk_xxx_id` int(11) NOT NULL,
`xxx_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `FK_2kma3uvjxartf5p9l24yk2fxu` (`fk_xxx_id`),
CONSTRAINT `FK_2kma3uvjxartf5p9l24yk2fxu` FOREIGN KEY (`fk_xxx_id`) REFERENCES `xxx_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
这会阻止迁移完成。
还验证了直接在mysql-console中的约束应用程序正常工作。
mysql> alter table client_xxxs add CONSTRAINT `x-FK_2kma3uvjxartf5p9l24yk2fxu` FOREIGN KEY (`fk_xxx_id`) REFERENCES `xxx_types` (`id`);
Query OK, 0 rows affected (0.81 sec)
Records: 0 Duplicates: 0 Warnings: 0
show create ...
CONSTRAINT `x-FK_2kma3uvjxartf5p9l24yk2fxu` FOREIGN KEY (`fk_xxx_id`) REFERENCES `xxx_types` (`id`),
答案 0 :(得分:0)
当Liquibase尝试添加约束时,mysql会抛出您看到的错误。我认为错误是由于基本列中的数据与引用列中的数据不匹配。我希望不会创建外键,但可能仍然使用mysql。
如果你运行select * from client_xxxs where fk_xxx_id not in (select id from xxx_types)
,你会得到任何结果吗?