Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27168 closed Bug (needsinfo)

"LookupError: App 'xxxxx' doesn't have a 'xxxxx' model" with custom table name

Reported by: John Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: migration
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by John)

Hi,

I recently wrote a migration script in order to migrate some date and one of the model uses a custom table name using db_table (let's call it "myapp_my_custom_table")

When I try to run the migration I got this error:
LookupError: App 'myapp' doesn't have a 'mymodel' model

After investigating, I found out that the model name MyModel (not the real one) is put in lower case then used as key to fetch the actual model class (
django.apps.config.AppConfig#get_model )

One of the item of the self.models is named after the db_table my_custom_table, instead of the model name mymodel

The solution I found is to pass the table name instead of the model name.

Before:

def migrate_data(apps, schema_editor):
    mymodel_model = apps.get_model(
        app_label='myapp',
        model_name='MyModel')

After:

def migrate_data(apps, schema_editor):
    mymodel_model = apps.get_model(
        app_label='myapp',
        model_name='my_custom_table')

I don't know if it's the correct behaviour.

Please advise.
Thanks

Change History (4)

comment:1 by John, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

I couldn't reproduce this on both master and Django 1.8.x. In both cases, model_name='MyModel' worked after adding a custom db_table. Could you provide a sample project that reproduces the issue?

comment:3 by John, 8 years ago

The original code (changed some of the code) is this:

def migrate_xxxxx(apps, schema_editor):
    my_model_model = apps.get_model(
        app_label='myapp',
        model_name='MyModel')

    my_other_model_model = apps.get_model(
        app_label='myotherapp',
        model_name='MyOtherModel')

    my_objects = my_model_model.objects.all()

    for my_object in my_objects:
        obj, created = my_other_model_model.objects.get_or_create(
            engagement=my_object.engagement,
            client=my_object.client
        )

My teammate wasn't able to reproduce it either.

My env is Python 2.7.11 with Django 1.8.12

This is the stack trace:

Operations to perform:
  Apply all migrations: billing
Running migrations:
  Rendering model states... DONE
  Applying billing.0003_create_missing_xxxxx...Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/project101/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/user/project101/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/project101/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/project101/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/user/project101/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/user/project101/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/user/project101/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/user/project101/lib/python2.7/site-packages/django/db/migrations/migration.py", line 112, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/user/project101/lib/python2.7/site-packages/django/db/migrations/operations/special.py", line 183, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/user/project101/project/billing/migrations/0003_create_missing_xxxxx.py", line 15, in migrate_client_billing
    model_name='EngagementClient')
  File "/home/user/project101/lib/python2.7/site-packages/django/apps/registry.py", line 202, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File "/home/user/project101/lib/python2.7/site-packages/django/apps/config.py", line 162, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'myapp' doesn't have a 'mymodel' model.
Last edited 8 years ago by John (previous) (diff)

comment:4 by Tim Graham, 8 years ago

Resolution: needsinfo
Status: newclosed

Okay, please reopen if you can provide us with details about how to reproduce it.

Note: See TracTickets for help on using tickets.
Back to Top