Mysql添加外键报错:Referencing column 'xx' and referenced column 'xx' in foreign key constraint 'xxx' are incompatible.


问题描述:

在生成迁移文件后,接着执行迁移,出现如下错误:

django.db.utils.OperationalError: (3780,Referencing column 'category_id' and referenced column 'id' in foreign key constraint 'tb_goods_visit_category_id_b3e36237_fk_tb_goods_category_id' are incompatible.)

问题翻译:

在外键约束'tb_goods_visit_category_id_b3e36237_fk_tb_goods_category_id'中引用列“category_id”和引用列“id”不兼容。

原因分析:

说明两个表关联的列数据类型不一致,比如:varchar 与 int,或者 int无符号 与 int有符号,都不行,必须要一模一样才可以。

我的报错是因为A表中的“category_id”为bigint类型,B表中的“id”为int类型,它俩不一致。

解决方法:

将所关联的两表中的相关两列的类型修改成一致的即可,就可以添加外键了,然后成功迁移数据了。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM