9

I have the following migration (logic removed for simplicity):

def migrate_existing_discounts(apps, _):
    ModelA = apps.get_model('myapp', 'ModelA')
    ModelB = apps.get_model('myapp', 'ModelB')

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0071_auto_20160531_1342'),
    ]

    operations = [
        migrations.RunPython(migrate_existing_discounts)
    ]

When running it the following exception rises:

LookupError: App 'myapp' doesn't have a 'modelb' model.

ModelA inherits from models.Model and it's successfully loaded. On the other hand, ModelB inherits from TranslatableModel and so it breaks. I've read that (2 years ago) migrations used to have problems loading abstract classes(ticket#21786 and ticket#21519), and TranslatableModel is one of.

I've had this problem before and I ended up migrating with RunSQL instead, but I would like to know how to import the models properly, since there must be a way.

Note: The package django-hvad doesn't have migrations so there isn't any dependencies to add.

dnaranjo
  • 3,647
  • 3
  • 27
  • 41
  • 2
    Which version of django are you using? Can you add the model code for ModelA and ModelB? And the full traceback for the LookupError. It seems strange that the error message says `modelb` in lowercase letters. – Håken Lid Jun 01 '16 at 09:25
  • Do the models have inheritance to the User model by any chance? – Andrey Shipilov Aug 15 '18 at 03:52

1 Answers1

0

If all of your migrations are running from start to finish, the models you are referencing might not exist yet in your new database. Update your dependencies list inside the migration to reference the last migration file on the app where those models are defined.

Nate
  • 321
  • 3
  • 6