The problem is slightly tricky to define so please bear with me.
I have a model called Blog. I have three migrations in it.
0001_initial: Create the table for Blog
0002_seed_data: Seed some data into this table. To seed this data, I use:
all_blogs = Blog.objects.all()
for blog in all_blogs.iterator():
#some logic here
blog.save()
0003_add_column: Add a new column 'title' to this table
When I run 0003_add_column on a database where 0002_seed_data has already run in the past, it works.
But when I run these migrations on an empty database, it fails at 0002_seed_data because it cannot find a title in the database. It gives an error similar to:
django.db.utils.OperationalError: (1054, "Unknown column 'Blog.title' in 'field list'")
This looks like a chicken an egg problem to me because 0002 cannot run until title column isn't present in the table. There is provision to add a title in the table in 0003, but that cannot run until 0002 has run. A simple solution is to correct the order. This is what I have been doing in the past. But this means everytime I add a new column to this table, its order has to be corrected.
A quick search of the error returns a few solutions and most of them talk about deleting all migrations and making them again using makemigrations. This will not solve my problem
So is there a better way of doing this?