'Django: How to migrate from ManyToMany to ForeignKey?
I'm building a REST API and server using Django and Django Rest Framework. We are using a postgres database.
I need to simplify a badly designed relation. We have a model (House) that has a ManyToMany relationship to another (City). In reality it would be enough when this is a ForeignKey relationship.
I googled and couldn't find any blog posts or docs how to properly migrate in this direction. I could only find the other way (FK to M2M).
I'm 98% sure that all the data on the server will be consistent with a FK relationship (meaning I'm pretty sure all houses only have one city). We need to change the relationship for several reasons and aren't able to keep the M2M.
I'm afraid to just change the model and running makemigrations and migrate. I was wondering, how do you properly migrate from M2M to FK? Are there any caveats I have to take into account? How can I deal with data if surprisingly there are houses with multiple city's? The data set is still quite small (less than 10k entries) if that matters.
Thank you very much.
Solution 1:[1]
Based on Timmy's answer here is what I did:
I added a field like this
city = models.ForeignKey(City, related_name='has_houses', blank=True, null=True)to avoid therelated_namefor reverse relations and to have the FK blank. Then I ranmakemigrationsandmigrate.Next, I ran
python manage.py makemigrations --empty housesbecause my app is namedhouses.I added the code for the migration (see below). Then I ran
migrate.I deleted the M2M field and the
related_name,nullandblankconstraints and ranmakemigrationsandmigrateone last time.
Code for the migration:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2019-02-15 09:09
from __future__ import unicode_literals
from django.db import migrations
def save_city_fk(apps, schema):
City = apps.get_model('cities', 'City')
House = apps.get_model('houses', 'House')
for house in House.objects.all():
house.city = house.cities.all().first()
house.save()
class Migration(migrations.Migration):
dependencies = [
('houses', '0003_auto_20190215_0949'),
]
operations = [
migrations.RunPython(save_city_fk),
]
Solution 2:[2]
First obviously you need to make sure (by making the appropriate query) that you really only have one city per house. If there are houses which have more than one city, you need to resolve the conflict by deleting cities from the relationship, splitting houses etc.
After that, you can do it in steps:
- create a new FK in
House, migrate and populate it with the one city id from the old m2m relationship - rename the m2m field, migrate, then give the new FK the name of the old m2m field if desired and migrate again
- check if your queries work and adapt them as necessary
- when satisfied that everything works, delete the old m2m field – only after migrating this step will you lose the ability to roll back the db
Solution 3:[3]
for M2M relation it is better to build a new MOdel to represent the relation. following is the example of an`employee having multiple designation and designation obtained by multiple employees
class Designation(models.Model):
desig_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=60, unique=True)
job_discription = models.CharField(max_length=2000)
def __str__(self):
return self.title
class Employee(AbstractUser):
middle_name = models.CharField(max_length=20, blank=True, null=True)
basic_salary = models.FloatField(default=1)
designation = models.ManyToManyField(Designation, default=None, blank=True,
through='EmployeeDesignation')
certification = models.ManyToManyField(Certification, default=None, null=True, blank=True,
through='EmployeeCertification')
emp_img = models.FileField(default=None,upload_to='employees')
leaves_allowed = models.IntegerField(default=25)
leave_balance = models.IntegerField(default=25)
leave_count = models.IntegerField(default=0)
objects = EmployeeManager()
def __str__(self):
return self.first_name+' '+self.last_name
class EmployeeDesignation(models.Model):
desig = models.ForeignKey(Designation, on_delete=models.CASCADE)
emp = models.ForeignKey(Employee, on_delete=models.CASCADE)
class Meta:
unique_together = (('emp', 'desig'),)
Solution 4:[4]
I have seen that the answers are saying that you have to create multiple migration files to do it but this is not great practice and after some time you may squash your migrations together. Just create an empty migration or create an automatic migration by using:
python manage.py makemigrations
and provide a default value for the existing instances.
After that, you can change the file and modify the changes and apply the data migration without losing any data. Create the functions and run them after adding the new fields and after that, you can delete them and rename the new field properly.
migrations.AddField(...),
migrations.RunPython(save_city_fk),
migrations.RemoveField(...),
migrations.AlterField(...),
They order is important.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | J. Hesters |
| Solution 2 | Endre Both |
| Solution 3 | Malik Shahzad |
| Solution 4 | Shayan |
