Django Migrations – Reversible migrations

Migrations is a great tool to management your models. Django has a powerful set of commands to propagating changes into your database schema.

Please see the official documentation for more details.

An interesting command is run the last migrations, for example:

You have tree migrations in app billing:, and

Note: The migrations is a data migration

Suppose that you need to revert the migrations and, you can run python migrate_schemas billing 0001.

If you did not write a reversible migrations this error will occur.

django.db.migrations.migration.IrreversibleError: Operation <RunPython <function set_default_author at 0x7f57b0455bf8>> in billing.0003_update_purchasable is not reversible

So, you need to pass the RunPython.noop method on parameter reverse_code when you want rollback operation without effects.

migrations.RunPython(gen_purchasable, reverse_code=migrations.RunPython.noop)


from __future__ import unicode_literals
from django.db import migrations, models

def gen_purchasable(apps, schema_editor):
    MyModel = apps.get_model('app', 'purchasable')
    for row in MyModel.objects.all():
    if row.enabled;

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0002_auto_20150622_1928'),

    operations = [
        migrations.RunPython(gen_purchasable, reverse_code=migrations.RunPython.noop),

If you need to revert the migration data, you need to write any function, for example:


Don’t forget to pass a reversible code in data migrations, If you dont require rollback data just pass reverse_code=migrations.RunPython.noop.

For more details on the RunPython operation, please see the docs.