# Swithing ForeignKey in an existing table

**URL:** <https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490>\
**Category:** Using the ORM\
**Created:** [December 26, 2024, 4:24am UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490 "2024-12-26T04:24:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 26, 2024, 4:24am UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490/1 "2024-12-26T04:24:08Z")

</div>

I have a class like this :

```auto
class someClass1(models.Model):
    # some field 1
    custom_vhd_azure_tbl = models.ForeignKey(Vhd_azure, on_delete=models.CASCADE)
    # some field n
    created_date = models.DateTimeField(auto_now_add=False, null=True)

```

I now realize a mistake was made : **custom\_vhd\_azure\_tbl** should’ve been FKed to **Custom\_vhd\_azure** instead.

`custom_vhd_azure_tbl = models.ForeignKey(Custom_vhd_azure, on_delete=models.CASCADE)`

Some 10 rows of data currently exits in someClass1 table for which it can be truncated.

But what I should do to make the field switch from one FK to another ?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [December 26, 2024, 11:14am UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490/2 "2024-12-26T11:14:26Z")

</div>

Does the `Custom_vhd_azure` model has any fk to `Vhd_azure` so you could relate one from the other?  
If there’s no relation, then you wont be able to keep the data on these rows

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 26, 2024, 11:18am UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490/3 "2024-12-26T11:18:43Z")

</div>

`Custom_vhd_azure` and `Vhd_azure` are 2 separate distinct tables that have no FKs.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [December 26, 2024, 12:01pm UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490/4 "2024-12-26T12:01:39Z")

</div>

Then you have no way of keeping this existing data.  
What you can do, if you have a way to, is:

1. Remove the field `custom_vhd_azure_tbl` from the model, run `makemigrations`. It will generate a RemoveField operation
2. Add the field `custom_vhd_azure_tbl` in the model for the correct model, run `makemigrations`. It will generate a AddField operation
3. Run `makemigrations your_app --empty` to generate a empty migration, that you can use to write a `RunPython` operation to populate the existing rows with the proper model for that foreign key.

Following these steps you can “keep” the field `custom_vhd_azure_tbl` with the new values.

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 27, 2024, 4:19am UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490/5 "2024-12-27T04:19:06Z")

</div>

How do I execute `RunPython` post empty makemigrations ?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [December 30, 2024, 10:43am UTC](https://forum.djangoproject.com/t/swithing-foreignkey-in-an-existing-table/37490/6 "2024-12-30T10:43:31Z")

</div>

You can find all possible migration operations under this section of the documentation: [Migration Operations](https://docs.djangoproject.com/en/5.1/ref/migration-operations/)  
And there you can find about [RunPython](https://docs.djangoproject.com/en/5.1/ref/migration-operations/#runpython)
