SS_Rebelious
SS_Rebelious

Reputation: 1364

Unable to import Django model from the other app during migration roll-back

I'm creating a data-migration for the new_app with the possibility to roll it back.

# This is `new_app` migration
class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.RunPython(import_data, reverse_code=delete_data)
    ]

This migration adds some data to the model defined in other app: my_other_app. To import the model where I want to update or delete records I use apps.get_model() method.

# This is `new_app` migration
def import_data(apps, schema_editor):   
    model = apps.get_model('my_other_app', 'MyModel')

It works like charm when I apply migrations. But when I run try to roll back the migration with the :~> manage.py migrate new_app zero I get exception: LookupError: No installed app with label 'my_other_app'. Model import in roll back code:

# This is `new_app` migration
def delete_data(apps, schema_editor):
    schema_model = apps.get_model('my_other_app', 'MyModel')

The code for model import is identical, but why it doesn't work during migration roll back? For now I have a workaround with straight model import during roll-back. Don't know if it may cause troubles in future.

Upvotes: 2

Views: 229

Answers (1)

meshy
meshy

Reputation: 9116

Make sure that dependencies includes the latest migration from the other app that you're referencing. eg:

dependencies = [
    'my_other_app.0001_initial',
]

Also, make sure 'my_other_app' is in your INSTALLED_APPS setting.

Upvotes: 5

Related Questions