# M2M field name change

**URL:** <https://forum.djangoproject.com/t/m2m-field-name-change/42770>\
**Category:** Using the ORM\
**Created:** [September 3, 2025, 6:47am UTC](https://forum.djangoproject.com/t/m2m-field-name-change/42770 "2025-09-03T06:47:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harryghgim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/harryghgim/32/5698_2.png) [@harryghgim](https://forum.djangoproject.com/u/harryghgim)\
**Post date:** [September 3, 2025, 6:47am UTC](https://forum.djangoproject.com/t/m2m-field-name-change/42770/1 "2025-09-03T06:47:33Z")

</div>

Hi Django fellows.

You have a model Booking that is referenced by another model by m2m field(Product.bookings). Now you want to change name of model Booking to Payment and Product.bookings should be change to payments.

When you run make migrations, it shows Remove field first and then Add field. If you want to keep data while change names, how would you do it seamlessly?

If there’s any related discussion, forward me please. Thank you.

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [September 3, 2025, 10:09am UTC](https://forum.djangoproject.com/t/m2m-field-name-change/42770/2 "2025-09-03T10:09:56Z")

</div>

Hi @harryghgim, what are you looking for is the [SeparateDatabaseAndState](https://docs.djangoproject.com/en/5.2/ref/migration-operations/#django.db.migrations.operations.SeparateDatabaseAndState) operation, the [example](https://docs.djangoproject.com/en/5.2/howto/writing-migrations/#changing-a-manytomanyfield-to-use-a-through-model) provided to help using it is a similar case.

You could perform a table rename rather than deleting it as database operation, while keeping the M2M field change as the state operation.

---

<div class="post-metadata">

**Author:** ![harryghgim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/harryghgim/32/5698_2.png) [@harryghgim](https://forum.djangoproject.com/u/harryghgim)\
**Post date:** [September 4, 2025, 1:38am UTC](https://forum.djangoproject.com/t/m2m-field-name-change/42770/3 "2025-09-04T01:38:21Z")

</div>

Hi @sevdog .

I ended up doing renaming Booking model and insert data _before_ m2m tables are deleted.

Generated migration files by command _first_ execute RemoveField on Product.bookings and then AddField Product.payments. Instead I put AddField first and then RemoveField later. Then between them I add RunSQL with insert statement, both in `sql` and `reverse_sql`.

Crucial thing is that I put _old table name_ when doing `INSERT INTO`, because by the time RunSQL is run new m2m table has already been created. That looked weird because the fields and table names are not in sync with final state, but that’s the way to go.

Frankly I did get some help from gen ai.
