[Solved] MYSQL add foreign key error: Referencing column ‘xx’ and referenced column ‘xx’ in foreign key constraint ‘xxx’ are incompatible.

Problem Description:

After the migration file is generated, the migration is performed, and the following error occurs:

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.)

Question translation:

In the foreign key constraint ‘tb_goods_visit_category_id_b3e36237_fk_tb_goods_category_id’, the reference column ‘category_id’ and the reference column ‘ID’ are incompatible.

Cause analysis:

It is as like as two peas, which are inconsistent with the two data types. For example, varchar and int, or int, unsigned and int are not allowed.

My error is reported because the “category_id” in table a is bigint and the “Id” in table B is int. they are inconsistent.

Solution:

Modify the types of the related two columns in the associated two tables to be consistent, then you can add foreign keys, and then migrate the data successfully.

Similar Posts: