Reputation: 15778
I have a migration like this:
class Migration(migrations.Migration):
dependencies = [
('app', '0020_auto_20191023_2245'),
]
operations = [
migrations.AddField(
model_name='agenda',
name='theme',
field=models.PositiveIntegerField(default=1),
),
]
But it raises an error:
django.db.utils.ProgrammingError: column "theme" of relation "app_agenda" already exists
Not a problem, I've wrapped this error like this:
from django.db import migrations, models, ProgrammingError
def add_field_theme_to_agenda(apps, schema_editor):
try:
migrations.AddField(
model_name='agenda',
name='theme',
field=models.PositiveIntegerField(default=1),
),
except ProgrammingError as e: # sometimes it can exist
if "already exists" not in str(e):
raise
class Migration(migrations.Migration):
dependencies = [
('app', '0020_auto_20191023_2245'),
]
operations = [
migrations.RunPython(add_field_theme_to_agenda),
]
This works like a charm and all the following migrations are done.
My problem is that each time I run a "makemigrations
" Django adds again the migration (= the one on the top of my question). I guess it's because it doesn't see it in the migrations, because my code obfuscate it.
How to circumvent this using migrations (dont say answers like "this problem is on your database, correct your database")?
Upvotes: 4
Views: 4381
Reputation: 5492
Django is re-creating the migrations because as you are doing the operation manually inside a RunPython operation, it can not understand the field is added. What you can try is (haven't tried this myself), subclass AddField operations to create a custom AddField operation, where you can handle the exception. Something like the following could work:
from django.db import migrations, models, ProgrammingError
class AddFieldIfNotExists(migrations.AddField):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
try:
super().database_forwards(app_label, schema_editor, from_state,
to_state)
except ProgrammingError as e: # sometimes it can exist
if "already exists" not in str(e):
raise
class Migration(migrations.Migration):
atomic = False
dependencies = [
('app', '0070_auto_20191023_1203'),
]
operations = [
AddFieldIfNotExists(
model_name='agenda',
name='theme',
field=models.PositiveIntegerField(default=1),
),
]
Upvotes: 9
Reputation: 1149
You can take advantage of the --fake
flag in your migrate command
./manage.py migrate app_name migration_number --fake
This will mark the migration as done.
Upvotes: 2