# Incompatible foreign key when attempting to link tables in Django?

**URL:** <https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650>\
**Category:** Using the ORM\
**Created:** [July 6, 2022, 9:18am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650 "2022-07-06T09:18:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shathamhb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shathamhb/32/6580_2.png) [@Shathamhb](https://forum.djangoproject.com/u/Shathamhb)\
**Post date:** [July 6, 2022, 9:18am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/1 "2022-07-06T09:18:05Z")

</div>

Hi Django Developer,

I’m trying to make a couple basic models , and link them with foreign Key. See the below code:

```auto
class CustomerInfo(models.Model):
    customer_name = models.CharField(max_length=255)
    id_no = models.CharField(max_length=255)
    mobile_no = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    status = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'customer_info'

```

```auto
class User(AbstractBaseUser):
    username = models.CharField(max_length=255, unique=True)
    user_fullname = models.CharField(max_length=500)
    role_id = models.ForeignKey(
        'Roles', on_delete=models.DO_NOTHING, db_column='role_id')
    #dept_id = models.CharField(max_length=500)
    is_head = models.IntegerField(blank=True, null=True)

```

I migrated this to the DB first. That worked fine, Django successfully created the new table in the database. And then I tried to make the reference via foreign key.

```auto
class User(AbstractBaseUser):
  
....
    customer_id = models.ForeignKey( 
        'CustomerInfo' , on_delete=models.DO_NOTHING, 
        db_column='customer_id' , blank=True, null=True)

```

but this doesn’t work. I get the following error:

```auto
django.db.utils.OperationalError: (3780, "Referencing column 'customer_id' and referenced column 'id' in foreign key constraint 'TS_user_customer_id_545183b3_fk_customer_info_id' are incompatible.")

```

I cannot figure out why this is happening because the referencing tool is successfully referencing multiple other tools, but it will not link to this one. What is the reason for this error? And how can I fix it so that I can link the tables? Why are they incompatible?

---

<div class="post-metadata">

**Author:** ![AhmdMWddh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmdmwddh/32/21707_2.png) [@AhmdMWddh](https://forum.djangoproject.com/u/AhmdMWddh)\
**Post date:** [July 6, 2022, 10:26am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/2 "2022-07-06T10:26:24Z")

</div>

> [@Shathamhb](#):
>
> ```auto
> class User(AbstractBaseUser):
>   
> ....
> customer_id = models.ForeignKey( 
> 'CustomerInfo' , on_delete=models.DO_NOTHING, 
> db_column='customer_id' , blank=True, null=True)
> 
> ```

Try to change customer\_id db\_column into `'user_customerinfo'`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 11:02am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/3 "2022-07-06T11:02:26Z")

</div>

Did you previously have this customer\_id column and are now changing it to a ForeignKey? (Otherwise, why are you specifying the `db_column` name?)

---

<div class="post-metadata">

**Author:** ![Shathamhb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shathamhb/32/6580_2.png) [@Shathamhb](https://forum.djangoproject.com/u/Shathamhb)\
**Post date:** [July 6, 2022, 11:16am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/4 "2022-07-06T11:16:40Z")

</div>

I tried but it seemed doesn’t work. I got the same error.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 11:24am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/5 "2022-07-06T11:24:04Z")

</div>

Also, you show `CustomerInfo` as `managed = False`. What is the primary key column of the `CustomerInfo` model?

---

<div class="post-metadata">

**Author:** ![Shathamhb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shathamhb/32/6580_2.png) [@Shathamhb](https://forum.djangoproject.com/u/Shathamhb)\
**Post date:** [July 6, 2022, 11:30am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/6 "2022-07-06T11:30:26Z")

</div>

Yes, I had this field `customer_id` in the other models.

Now I change db\_column name

```auto
user_customerinfo = models.ForeignKey( 
        'CustomerInfo' , on_delete=models.DO_NOTHING, 
        db_column='user_customerinfo' , blank=True, null=True)

```

I followed @AhmdMWddh and tried it. But I have the same problem.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 11:33am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/7 "2022-07-06T11:33:16Z")

</div>

What column is the primary key of `CustomerInfo`?

(Or, more accurately since you don’t specify it in your model, what column is the primary key of the `customer_info` table in the database?)

---

<div class="post-metadata">

**Author:** ![Shathamhb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shathamhb/32/6580_2.png) [@Shathamhb](https://forum.djangoproject.com/u/Shathamhb)\
**Post date:** [July 6, 2022, 11:34am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/8 "2022-07-06T11:34:29Z")

</div>

![foreign#4](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/a/ae4ab15f87782cbe5bd6d48cb02f57e7e44fd476.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 11:35am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/9 "2022-07-06T11:35:10Z")

</div>

What version of Django are you using?

---

<div class="post-metadata">

**Author:** ![Shathamhb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shathamhb/32/6580_2.png) [@Shathamhb](https://forum.djangoproject.com/u/Shathamhb)\
**Post date:** [July 6, 2022, 11:37am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/10 "2022-07-06T11:37:47Z")

</div>

the version of Django is 4.0.3

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 11:44am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/11 "2022-07-06T11:44:02Z")

</div>

I think the first thing I’d try would be to simplify the definition to see if there’s some sort of conflict among the options you’ve select.

I would do the following to try and get this straightened out:

- Remove the customer\_id column from the model

- makemigrations / migrate

- Check the database to see if the `customer_id` column exists in the `User` table.

- Add the following definition to the `User` model:  
`customer = models.ForeignKey('CustomerInfo', on_delete=models.PROTECT, null=True)`

- makemigrations / migrate

---

<div class="post-metadata">

**Author:** ![oneflydown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/oneflydown/32/9123_2.png) [@oneflydown](https://forum.djangoproject.com/u/oneflydown)\
**Post date:** [July 6, 2022, 1:50pm UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/12 "2022-07-06T13:50:33Z")

</div>

> [@AhmdMWddh](#):
>
> Try to change customer\_id db\_column into `'user_customerinfo'`

This will not work because there is no `customerinfo` field on `User` table.

> [@Shathamhb](#):
>
> ```auto
> class User(AbstractBaseUser):
>   
> ....
> customer_id = models.ForeignKey( 
> 'CustomerInfo' , on_delete=models.DO_NOTHING, 
> db_column='customer_id' , blank=True, null=True)
> 
> ```

Since you are specifying foreign key relation to `CustomerInfo` table, the available db\_columns are `id` and `id_no` and not `customer_id`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 1:56pm UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/13 "2022-07-06T13:56:47Z")

</div>

I think you’re misunderstanding the function of the `db_column` parameter. It’s a reference to the column name in the User table, not the column name being referenced in the CustomerInfo model.

---

<div class="post-metadata">

**Author:** ![oneflydown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/oneflydown/32/9123_2.png) [@oneflydown](https://forum.djangoproject.com/u/oneflydown)\
**Post date:** [July 6, 2022, 2:08pm UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/14 "2022-07-06T14:08:22Z")

</div>

Yes, looks like. If the user model is changed after the first migration of the app (`0001_initial`), manual fixing of schemas is required since foreign keys and many-to-many relationships are affected.

Outline of the steps required are given here [Changing to a custom user model mid-project](https://code.djangoproject.com/ticket/25313)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2022, 2:16pm UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/15 "2022-07-06T14:16:38Z")

</div>

> [@oneflydown](#):
>
> If the user model is changed after the first migration of the app (`0001_initial`), manual fixing of schemas is required since foreign keys and many-to-many relationships are affected.

That’s only true if you’re changing to a different Model. Making a change to the currently-used User model is ok.

(In other words, if you’re using the default User model and then switching to a user-defined User model you would need to follow those steps. But if you are starting with a user-defined User model, you can make changes to it as desired without taking extra steps.)

But that’s a different situation than the issue here.

For some yet-to-be-identified reason, Django migrations is not matching the data type of the related field.

My guess is that this is because the target table is `managed = False`.

It may be necessary to edit the migration file for this User model to force the right data type.

---

<div class="post-metadata">

**Author:** ![Shathamhb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shathamhb/32/6580_2.png) [@Shathamhb](https://forum.djangoproject.com/u/Shathamhb)\
**Post date:** [July 19, 2022, 11:59am UTC](https://forum.djangoproject.com/t/incompatible-foreign-key-when-attempting-to-link-tables-in-django/14650/16 "2022-07-19T11:59:36Z")

</div>

Thank you so much for your time; I really appreciate it.

I figured out what was going on:  
The column of `customer_id` type is **BIGINT** and the references `id` (customerInfo) type is **INT**

So I fixed that by changing one of the column types to match the other will allow you to create the table with the foreign key constraint.
