# Nullable foreignKey upon existing data

**URL:** <https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861>\
**Category:** Using the ORM\
**Created:** [July 18, 2022, 7:13am UTC](https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861 "2022-07-18T07:13:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![meerakhanna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/meerakhanna/32/9241_2.png) [@meerakhanna](https://forum.djangoproject.com/u/meerakhanna)\
**Post date:** [July 18, 2022, 7:13am UTC](https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861/1 "2022-07-18T07:13:53Z")

</div>

I am stuck with this issue for longer time. Scenario is, there is a table(List) with 1000s of rows. Now I am trying to make one of its column (state) to be a foreignKey linking with another table(State) which is too having data around 20 rows.  
But Django is not allowing me to proceed with making ‘migrate’ stating the error message like the below.

`django django.db.utils.IntegrityError: The row in table 'List' with primary key '2' has an invalid foreign key: Dapp.List contains a value '' that does not have a corresponding value in State.state.`

the field ‘state’ is nullable with the table ‘List’ and does have null values in the column for many of its rows.  
But the linked ‘State’ doesn’t have any Null value.

following is the change I did in the model before trying to migrate,

`State = models.ForeignKey(State, blank=True, null=True, on_delete=models.SET_NULL)`

I am not getting a solution for this issue. Could you please help? Django version 4.0.5,

---

<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 18, 2022, 11:12am UTC](https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861/2 "2022-07-18T11:12:00Z")

</div>

Are you trying to add a ForeignKey field here or are you trying to change an existing field to be a ForeignKey?

(I generally discourage trying to do the latter. I’ve found it safer to create a new field for the ForeignKey, then write a quick script to assign the right value to that field (or do it in the shell), then proceed from that point.)

Note that the empty (or blank) string `''` is not the same as `Null`.

Also note that a ForeignKey either refers to the Primary Key of the related table, or to another field that is defined with `unique=True`.

---

<div class="post-metadata">

**Author:** ![meerakhanna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/meerakhanna/32/9241_2.png) [@meerakhanna](https://forum.djangoproject.com/u/meerakhanna)\
**Post date:** [July 18, 2022, 11:43am UTC](https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861/3 "2022-07-18T11:43:06Z")

</div>

Actually I am working for a project of upgrading an existing solution in Django. So all the tables and relations are already existing in the old solution too.  
Yes, the ForeignKey is the primary key of that table, which is of unique values and yes, I am changing the existing field into Foreign key.

So no solution to the current issue?

---

<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 18, 2022, 11:47am UTC](https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861/4 "2022-07-18T11:47:41Z")

</div>

Yes, there is.

- Create a new field for the Foreign Key, either with a default or null=True

- Assign the appropriate references to that field

or -

- Update the existing field with appropriate references.

---

<div class="post-metadata">

**Author:** ![meerakhanna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/meerakhanna/32/9241_2.png) [@meerakhanna](https://forum.djangoproject.com/u/meerakhanna)\
**Post date:** [July 18, 2022, 1:46pm UTC](https://forum.djangoproject.com/t/nullable-foreignkey-upon-existing-data/14861/5 "2022-07-18T13:46:52Z")

</div>

> [@KenWhitesell](#):
>
> Note that the empty (or blank) string `''` is not the same as `Null`.

Thanks for that line Ken! ++ for the solutions too !!
