#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 )
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 , 8 years ago
Description: | modified (diff) |
---|
comment:2 by , 8 years ago
comment:3 by , 8 years ago
The original code is this:
def migrate_client_billing(apps, schema_editor): engagement_client_model = apps.get_model( app_label='engagement', model_name='EngagementClient') client_billing_model = apps.get_model( app_label='billing', model_name='ClientBilling') engagement_clients = engagement_client_model.objects.all() for engagement_client in engagement_clients: obj, created = client_billing_model.objects.get_or_create( engagement=engagement_client.engagement, client=engagement_client.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_client_billing...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_client_billing.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 'engagement' doesn't have a 'engagementclient' model.
comment:4 by , 8 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Okay, please reopen if you can provide us with details about how to reproduce it.
I couldn't reproduce this on both master and Django 1.8.x. In both cases,
model_name='MyModel'
worked after adding a customdb_table
. Could you provide a sample project that reproduces the issue?