#21786 closed Bug (fixed)

migrate fails with certain mixin/model combinations

Reported by: gabejackson Owned by: andrewgodwin
Component: Migrations Version: master
Severity: Normal Keywords: migrations
Cc: timo Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

manage migrate fails with:

Operations to perform:
  Synchronize unmigrated apps: admin, contenttypes, langapp, auth, sessions
  Apply all migrations: (none)
Synchronizing apps without migrations:
  Creating tables...
    Creating table django_admin_log
    Creating table auth_permission
    Creating table auth_group_permissions
    Creating table auth_group
    Creating table auth_user_groups
    Creating table auth_user_user_permissions
    Creating table auth_user
    Creating table django_content_type
    Creating table django_session
    Creating table langapp_mymodela_translation
    Creating table langapp_mymodela
    Creating table langapp_mymodelb_translation
    Creating table langapp_mymodelb
    Creating table langapp_mymodelc
    Creating table langapp_mymodeld
  Installing custom SQL...
  Installing indexes...
Installed 0 object(s) from 0 fixture(s)
Running migrations:
  No migrations needed.
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/core/management/base.py", line 244, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/core/management/base.py", line 291, in execute
    output = self.handle(*args, **options)
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 140, in handle
    changes = autodetector.changes(graph=executor.loader.graph)
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 33, in changes
    changes = self._detect_changes()
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 62, in _detect_changes
    if not new_apps.get_model(al, mn)._meta.proxy
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/apps/registry.py", line 183, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File "/Users/gabejackson/venv/hvad-test/lib/python2.7/site-packages/django/apps/base.py", line 125, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'langapp' doesn't have a 'mymodela' model.

given the following models.py:

from django.db import models
from hvad.models import TranslatableModel, TranslatedFields

class MyMixin(object):
    pass

# Fails
class MyModelA(TranslatableModel, MyMixin):
    translations = TranslatedFields(
        name=models.CharField(max_length=60),
    )

# Works
class MyModelB(TranslatableModel):
    translations = TranslatedFields(
        name=models.CharField(max_length=60),
    )

# Works
class MyModelC(models.Model, MyMixin):
    name=models.CharField(max_length=60),

# Works obviously
class MyModelD(models.Model):
    name=models.CharField(max_length=60),

Removing MyMixin from MyModelA and re-running migrate works (deleted the sqlite db first):

Operations to perform:
  Synchronize unmigrated apps: admin, contenttypes, langapp, auth, sessions
  Apply all migrations: (none)
Synchronizing apps without migrations:
  Creating tables...
    Creating table django_admin_log
    Creating table auth_permission
    Creating table auth_group_permissions
    Creating table auth_group
    Creating table auth_user_groups
    Creating table auth_user_user_permissions
    Creating table auth_user
    Creating table django_content_type
    Creating table django_session
    Creating table langapp_mymodela_translation
    Creating table langapp_mymodela
    Creating table langapp_mymodelb_translation
    Creating table langapp_mymodelb
    Creating table langapp_mymodelc
    Creating table langapp_mymodeld
  Installing custom SQL...
  Installing indexes...
Installed 0 object(s) from 0 fixture(s)
Running migrations:
  No migrations needed.

You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): no

These tests have been conducted with hvad master (github) and django master (github)

Change History (6)

comment:1 Changed 18 months ago by gabejackson

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

posted after discussion with andrewgodwin in #django-dev

some thoughts:

  • migrate still runs models through a serialise/deserialise cycle
  • it runs autodetect to see if you have changes
  • perhaps not serialising properly

comment:2 Changed 18 months ago by andrewgodwin

  • Owner set to andrewgodwin
  • Status changed from new to assigned

comment:3 Changed 18 months ago by timo

  • Cc timo added
  • Triage Stage changed from Unreviewed to Accepted

I also ran across a similar issue but didn't distill it to a simple example like Gabe has done.

comment:4 Changed 18 months ago by andrewgodwin

I can't replicate this, as django-hvad doesn't work against 1.7 trunk. Can it be reduced to something that doesn't depend on an external app?

comment:5 Changed 18 months ago by andrewgodwin

Nevermind, I managed to get it working enough to debug the issue - migrations ignores any abstract bases (which TranslatableModel is one of), and if there's no bases at all adds models.Model as a base, but if you had a mixin it had bases but none were from Model. I've pushed a fix.

Looking at the hvad code, I wouldn't be surprised if it needs other updates for 1.7 as it's doing some weird things with models, but that's to be expected, I guess. It at least doesn't immediately fail now.

comment:6 Changed 18 months ago by Andrew Godwin <andrew@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 1f5268a01f82c314b48d195b7d7a344f9db58b6d:

Fixed #21786: Bad behaviour when mixins + abstract bases for migrations

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