Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#22833 closed Bug (fixed)

1.7 migrations can't delete codependent "through" models

Reported by: mozumder@… Owned by: nobody
Component: Migrations Version: 1.7-beta-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,

I'm using Django 1.7b4, and I'm testing the migrations. I have this existing model in the database:

class BlocksList(Named, Ordered):
    blocks_list_assignments = models.ManyToManyField(
                                          Block,
                                          through='BlocksListAssignment'
                                          )
    family = models.ForeignKey(
        BlockFamily,
        verbose_name=_("Family Name"),
        null=True,
        blank=True,
        )
    def __str__(self):  # Python 3: def __str__(self):
        return self.name


class BlocksListAssignment(Ordered):
    block = models.ForeignKey(Block)
    block_list = models.ForeignKey(BlocksList)
    def __str__(self):  # Python 3: def __str__(self):
        return self.block_list.name + ": %s" % self.block

I'm trying to delete this with the makemigrations/migrate command, with this in my migrations file:

        migrations.DeleteModel(
            name='BlocksList',
        ),
        migrations.DeleteModel(
            name='BlocksListAssignment',
        ),

It looks like the co-dependencies prevent the migrations from deleting both models, because I constantly get:

Running migrations:
  Applying blocks.0014_auto_20140613_2227...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: 'blockslistassignment'

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 76, 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 'blocks' doesn't have a 'blockslistassignment' 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 427, 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 419, 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 146, 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 90, in apply_migration
    if self.detect_soft_applied(migration):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 134, in detect_soft_applied
    apps = project_state.render()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py", line 86, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field blocks.BlocksList.blocks_list_assignments: blocks.BlocksListAssignment

Anyways to get around this I have to recreate the database and start from fixtures.

Do let me know if there is another way where I can use the migrations directly.

This is on Mac OS X 10.9, Python 3.4.0, Django 1.7b4, and PostGRES 9.3.4 database.

Change History (3)

comment:1 by Baptiste Mispelon, 11 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Hi,

I can indeed reproduce your issue with the following simplified models (the problem appears on master as well):

from django.db import models

class Foo(models.Model):
    bazes = models.ManyToManyField('Baz', through='Bar')


class Bar(models.Model):
    foo = models.ForeignKey('Foo')
    baz = models.ForeignKey('Baz')


class Baz(models.Model):
    pass

After running an initial makemigrations && migrate, I delete the first two models, then run makemigrations (which works, producing essentially the same migration as the one you describe). After that, running migrate triggers a KeyError like the one you reported.

I'll bump the severity to release blocker since this use-case should be perfectly valid.

Thanks.

comment:2 by Andrew Godwin <andrew@…>, 11 years ago

Resolution: fixed
Status: newclosed

In f717ef083aac2e839a86de1bac02b95e5fd1a4b1:

Fixed #22833: Autodetector not doing through mapping correctly

comment:3 by Andrew Godwin <andrew@…>, 11 years ago

In 13aa079941dcfd953ba03a83f55c8a14399d15c2:

[1.7.x] Fixed #22833: Autodetector not doing through mapping correctly

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