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.
- 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
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()
Of course, our motive is to change the model where the
IntegerField becomes a
class Manufacturer(models.Model): pass class Car(models.Model): manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE, related_name="manufacturer")
This can be achieved in 2 steps:
We will modify the
class Car(models.Model): manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE, related_name="manufacturer")
We will manually create a migration file under
# 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
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!