Liquibase异常 mysql数据库 Cannot add foreign key constraint
2017-08-30 10:25
411 查看
Caused by: java.sql.SQLException: Cannot add foreign key constraint
查看DATABASECHANGELOG 日志
使用h2数据库时,liquibase是能够正确创建表和外键以及索引,但是换到mysql的时候,无法成功创建外键。手动执行添加外键也无法成功
手动执行sql语句
报错
错误代码: 1215
Cannot add foreign key constraint
既然liquibase能够在h2上成功创建表以及外键,但是在mysql上创建不了,而且表存在手动也不能添加外键,估计就是mysql的原因了。
Mysql添加不了外键原因有三:
(1)外键对应的字段数据类型不一致
(2)两张表的存储引擎不一致
(3)设置外键时“删除时”设置为“SET NULL”
一个个排除,发现table_monitor_strategy这张表对应account_monitor_strategy这张表的外键字段是设计成varchar类型的,而account_monitor_strategy中的id是bigint型的,所以才会出现无法创建外键的问题。
这个问题可能是mysql特有的,在h2上依然能够成功创建外键。
查看DATABASECHANGELOG 日志
使用h2数据库时,liquibase是能够正确创建表和外键以及索引,但是换到mysql的时候,无法成功创建外键。手动执行添加外键也无法成功
手动执行sql语句
ALTER TABLE datasync_monitor.table_monitor_strategy ADD CONSTRAINT fk_account_table FOREIGN KEY (account_monitor_strategy_id) REFERENCES datasync_monitor.account_monitor_strategy (id)
报错
错误代码: 1215
Cannot add foreign key constraint
既然liquibase能够在h2上成功创建表以及外键,但是在mysql上创建不了,而且表存在手动也不能添加外键,估计就是mysql的原因了。
Mysql添加不了外键原因有三:
(1)外键对应的字段数据类型不一致
(2)两张表的存储引擎不一致
(3)设置外键时“删除时”设置为“SET NULL”
一个个排除,发现table_monitor_strategy这张表对应account_monitor_strategy这张表的外键字段是设计成varchar类型的,而account_monitor_strategy中的id是bigint型的,所以才会出现无法创建外键的问题。
这个问题可能是mysql特有的,在h2上依然能够成功创建外键。
相关文章推荐
- 异常处理:1215 - Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint
- django admin报错IntegrityError:'Cannot add or update a child row: a foreign key constraint fails'
- jbpm4 jbpm4_execution “Cannot delete or update a parent row: a foreign key constraint fails” 异常解决
- Mysql学习之cannot add foreign key constraint
- 1215-Cannot add foreign key constraint
- mysql无法添加外键约束(cannot add foreign key constraint)
- Cannot add or update a child row: a foreign key constraint fails 解决办法
- Hibernate报错 Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint 解决方法
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- [转][Err] 1452 - Cannot add or update a child row: a foreign key constraint fail
- 1215 - Cannot add foreign key constraint
- 刷新数据库总是报错:不能增加或者更新约束, Cannot add or update a child row: a foreign key constraint fails
- Cannot add foreign key constraint常见原因总结
- 关于mysql创表的时候出现错误:cannot add foreign key constraint
- 错误代码: 1215 Cannot add foreign key constraint
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- Error 'Cannot add or update a child row: a foreign key constraint fails