Django model relationships without running migration on existing database

idrisrampurawala

Idris Rampurawala

Posted on July 29, 2019

Django model relationships without running migration on existing database

In my previous post, we discussed about how we can create Django models of existing database with the command inspectdb. Extending to that post, in this post, we will try to create a foreign key relationship between models where our existing tables do not have foreign key constraints. Also, as it is legacy system, our use-case is to not run migrations of these constraints.

Problem

  • We have created Django models of our existing database
  • Our existing database do not have any foreign key constraint on any table
  • We want to utilize Django-ORM to query related tables
  • We do not want to migrate/create these constraints in existing database, so our database will be untouched

Solution

To achieve this, let's consider following Django models as an example

class Manufacturer(models.Model):
    pass

class Car(models.Model):
    manufacturer_id = models.IntegerField()
Enter fullscreen mode Exit fullscreen mode

Of course, our motive is to change the model where the manufacturer_id IntegerField becomes a manufacturer ForeignKey i.e.

class Manufacturer(models.Model):
    pass

class Car(models.Model):
    manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE,
                                     related_name="manufacturer")
Enter fullscreen mode Exit fullscreen mode

This can be achieved in 2 steps:

Step 1 - Modify the field in Django model

We will modify the IntegerField to ForeignKey field

class Car(models.Model):
    manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE,
                                     related_name="manufacturer")
Enter fullscreen mode Exit fullscreen mode

Step 2 - Add dummy migration entry

We will manually create a migration file under appName/migrations

# Generated manually

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        # Car - Manufacturer Relation
        migrations.AlterField(
            model_name='Car',
            name='manufacturer_id',
            field=models.IntegerField(db_column='manufacturer_id')
        ),
        migrations.RenameField(
            model_name='Car',
            old_name='manufacturer_id',
            new_name='manufacturer',
        ),
        migrations.AlterField(
            model_name='Car',
            name='manufacturer',
            field=models.ForeignKey(
                to='appName.Manufacturer', on_delete=models.CASCADE),
        ),
    ]
# Note: Change appName to your app name consisting the models
Enter fullscreen mode Exit fullscreen mode

Once you create and save this migration file, you can now access the related model. Remember, we are not running migrations on database!
The trick here is that, we are just letting the Django know about the Models relations but in reality it does not exist.

There might be multiple ways of achieving the result, but rest assure, I have tried this on production as well.

I hope this will be useful!

💖 💪 🙅 🚩
idrisrampurawala
Idris Rampurawala

Posted on July 29, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related