[Django]-How to ignore a specific migration?

7👍

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),
        ),
    ]

2👍

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.

Leave a comment