Python

Python

Made by DeepSource

Missing backward migration PTC-W0910

Bug risk
Major
Autofix

RunPython operation is missing reverse_code argument. If not provided, any attempt to undo the migration will raise IrreversibleError. Failure to reverse migrations will prevent going back to a previous state of the database if needed. Therefore, it is recommended to always include a backward/reverse callable so that the migration is reversible.

It is highly recommended to provide a function to the reverse_code parameter that undoes the migration. If it is safe to skip the migration to go to a previous state, you can specify RunPython.noop as a value. This will prevent Django from raising IrreversibleError.

Bad practice

from django.db import migrations


def update_marks(apps, schema_editor):
    pass

class Migration(migrations.Migration):
    dependencies = [("0002_field_with_default_value", "0001_no_issue")]
    operations = [
        migrations.RunPython(update_marks), # Issue
    ]

Preferred

from django.db import migrations


def update_marks(apps, schema_editor):
    pass

class Migration(migrations.Migration):
    dependencies = [("0002_field_with_default_value", "0001_no_issue")]
    operations = [
        migrations.RunPython(update_marks, reverse_code=migrations.RunPython.noop),
    ]

References