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")?