In my opinion, the complete destruction of migrations is a lazy and bad idea. I made snafu like @Afiz Momin and was able to dig myself out.
I had the following setup:
- Basic abstract model.
- Override
id field after creation (bad idea) - All other subclasses initiated Django migrations to try to create an ID field from the parent class.
So I got the following every time I ran makemigrations :
You are trying to add a non-nullable field 'id' to <tablename> without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py
But there was a way out.
In my first attempt, I just ran a fake value and then deleted the id column created, but that was clearly unstable. But on a whim, I did the following:
- Backing up the
migrations.orig directory to migrations.orig . - Run
./manage.py makemigrations to complete all migrations. - Check out the new migration file
0001-initial .
Most likely, the model she complains about ( <tablename> ) does not contain vital information. The bases argument was missing for me:
migrations.CreateModel( name='<ModelName>', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('some_field', models.IntegerField()),
- Now go back to
migrations.orig and find the migration where the model is first created . The migration line will be similar to the one above. - When you find the line in
migrations.orig , change it to match the new migration code. - Restore the original migrations. Remove
migrations and rename migrations.orig to migrations . - Just for good measure, run
./manage.py makemigrations and ./manage.py migrate . You should no longer receive error messages! - If he complains about another model, follow the procedure described.
source share