I have an existing model that looks somewhat like the following...
class Resource(models.Model):
id = models.AutoField(primary_key=True)
We have been using this for some time, and now have ~1M instances of these Resource
objects (and associated ForeignKey/else usages) in our database.
I now have a need to track another ID on this model, one that I want to enforce is unique.
other_id = models.IntegerField(unique=True)
This other_id
information is currently stored in some external CSVs, and I want to (at some point in the process) load this information in to all existing Resource
instances.
After adding the above field, Django's makemigrations
works just fine. However when I go to apply said migration against an existing database I get an error indicating that I need to provide a default to use for all existing Resource
instances. I'm sure many of you have seen something similar.
What is the best approach to getting around this limitation? Some methods I have thought of...
- Remove the
unique=True
requirement - apply the migration
- externally load in the
other_id
value to all existing models (through some management command, or 1-off script) - add the
unique=True
back in and apply the migration
- Remove the
- Dump all existing data to JSON
- flush all tables
- apply the migration (with unique=True)
- write a script that loads the data back in, adding the correct
other_id
value
- (unsure if this is possible) - Write some custom migration logic to automatically reference these external CSVs to load
other_id
values when I runmanage.py migrate
. This could hit issues if (at some point in the future) someone re-runs these migrations and this part fails (cannot find existing resourceid
in the CSVs to pull outother_id
).
All of these feel complicated, but then again I guess what I am trying to do isn't the simplest thing either.
Any ideas? I have to imagine someone has had to work around a similar issue in the past.
Thanks!