Opened 4 years ago

Closed 4 years ago

#22944 closed Bug (fixed)

Migrate doesn't like ForeignKey being reassigned to a different app's model

Reported by: mozumder@… Owned by: Andrew Godwin
Component: Migrations Version: 1.7-rc-2
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

Django 1.7 RC1 doesn't like it when I try to migrate an app's models.

I have a couple of models in App "Background". Simplified example:

class Family(models.Model):
    name = models.CharField(
                        _("name"),
                        max_length=255,
                        blank=True,
                        null=True,
                        )
    order = models.PositiveSmallIntegerField(
                    _("order"),
                    default=0,
                    )
    parent = models.ForeignKey(
                        'self',
                        blank=True,
                        null=True,


class ImageLayer(models.Model):

    family = models.ForeignKey(
        Family,
        verbose_name=_("Family Name"),
        related_name='background_family_name',
        null=True,
        blank=True,
        )


I have another separate App, "Styles" with a similar Family model:

class StyleFamily(models.Model):
    name = models.CharField(
                        _("name"),
                        max_length=255,
                        blank=True,
                        null=True,
                        )
    order = models.PositiveSmallIntegerField(
                    _("order"),
                    default=0,
                    )
    parent = models.ForeignKey(
                        'self',
                        blank=True,
                        null=True,
                    )

Now, I try to delete my "Family" model from the Background App, and reassign the family field to 'styles.StyleFamily" as:

class ImageLayer(models.Model):

    family = models.ForeignKey(
        'styles.StyleFamily',
        verbose_name=_("Family Name"),
        related_name='background_family_name',
        null=True,
        blank=True,
        )

Then I run manage.py makemigration and manage.py migrate

I get the following:

bobby:engine mozumder$ ./manage.py makemigrations
Migrations for 'background':
  0002_auto_20140702_1621.py:
    - Remove field parent from family
    - Delete model Family
    - Alter field family on imagelayer
    - Alter field family on stack
bobby:engine mozumder$ ./manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: grappelli
  Apply all migrations: sessions, styles, background, blocks, borders, defaults, effects, structure, transform, sites, contenttypes, admin, typography, content, animation, auth, colors, redirects, layout, text, articles, categories
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying background.0002_auto_20140702_1621...Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 152, in get_model
    return self.models[model_name.lower()]
KeyError: 'family'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py", line 79, in render
    model = self.apps.get_model(lookup_model[0], lookup_model[1])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py", line 190, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 155, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'background' doesn't have a 'family' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 118, in database_forwards
    from_model = from_state.render().get_model(app_label, self.model_name)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py", line 89, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field background.ImageLayer.family: background.Family

It seems it doesn't like it when I reassign the "family" field to a ForeignKey outside the App. This happens even with empty "Family" and "styles.Family" models.

I avoid this situation by rebuilding the database from scratch and calling in fixtures. I think this is probably a bug?

This is with Django 1.7 RC1, Python 3.4.0, and Postgres 9.3.4

Anyways, let me know if there is a workaround... thank you!

-bobby

Change History (11)

comment:1 Changed 4 years ago by anonymous

Oh I'll add that the initial "Family" model is based on mixins from another App, engine/globals.py:

from engine.globals import Named, Ordered, Family, FamilyMember

class Family(Family):
    class Meta:
        verbose_name_plural = "Families"

with engine/globals.py having the following:

class Ordered(models.Model):

    order = models.PositiveSmallIntegerField(
                    _("order"),
                    default=0,
                    )
    class Meta:
        abstract = True
        ordering = ['order']

class Named(models.Model):
    name = models.CharField(
                        _("name"),
                        max_length=255,
                        blank=True,
                        null=True,
                        )
    def get_name(self):  # Python 3: def __str__(self):
        return self.name
    def __str__(self):  # Python 3: def __str__(self):
        return "%s" % self.name
    class Meta:
        abstract = True


class Family(Named, Ordered):
    parent = models.ForeignKey(
                        'self',
                        blank=True,
                        null=True,
                        )
    def get_family(self):  # Python 3: def __str__(self):
        return self.name
    def __str__(self):  # Python 3: def __str__(self):
        if self.parent:
            return "%s (%s)" % (self.name, self.parent.name)
        else:
            return "%s" % self.name
    class Meta:
        abstract = True
        verbose_name_plural = "Families"


class FamilyMember():
    def get_family(self):  # Python 3: def __str__(self):
        return self.family.get_name()

Not sure if that affects expected behavior...

comment:2 Changed 4 years ago by Tim Graham

Possibly related/duplicate of #22970.

comment:3 Changed 4 years ago by anonymous

I have same problem after commit fe5f29e.
It's not related/duplicate of #22970. :(

comment:4 Changed 4 years ago by shawn.holyoak@…

I get the same error on 1.7 RC1. I have two apps, both created pre 1.7, that had no reference to each other. I made migrations for both, and applied them (Faked correctly). Then I changed the foreign keys in one app to point to a model in the second app. I get ValueError: Lookup failed for model referenced by field tracking.PropertyDescription.verified_by: employees.Employee. I asked a question on the Google group https://groups.google.com/forum/#!topic/django-users/9wgCcuSdVmo, which specifies a bit more detail, but today I created a brand new project to test this and received the exact same error. I don't know if it's exactly the same issue, but it happens consistently. I'll be glad to file another bug if it isn't the same issue.

comment:5 Changed 4 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:6 Changed 4 years ago by Andrew Godwin

Owner: changed from nobody to Andrew Godwin
Status: newassigned

Looks like the cause of this is the autodetector not knowing that foreign key alters should come before deletion of the model they used to point to. Since there are no steps to reproduce or example project, I'll fix that bug and mark this as closed and then if it still persists it can be reopened.

comment:7 Changed 4 years ago by Andrew Godwin <andrew@…>

Resolution: fixed
Status: assignedclosed

In 805774df1f2ec9552f1d9c826f5ca9276684da11:

Fixed #22944: Bad dependency on FK alteration in autodetector

comment:8 Changed 4 years ago by Andrew Godwin <andrew@…>

In 7e708a2536d4828d06f7aa4002fbd5cfc20bef16:

[1.7.x] Fixed #22944: Bad dependency on FK alteration in autodetector

comment:9 Changed 4 years ago by nikita@…

I get the same error on 1.7 RC2. Here is my code https://github.com/shultais/bug22944

Error with FK and M2M too.

comment:10 Changed 4 years ago by Andrew Godwin

Resolution: fixed
Status: closednew
Version: 1.7-rc-11.7-rc-2

Confirmed. Reopening.

comment:11 Changed 4 years ago by Andrew Godwin

Resolution: fixed
Status: newclosed

OK, this bug is different to the one originally reported here, and looks to be the same as #23100. Re-closing this and transferring to that one as the main tracking bug.

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