Opened 9 years ago

Closed 9 years ago

#22735 closed Bug (fixed)

1.7b4 - Wrong migration dependency calculation when models.ForeignKey "to" field specified as string

Reported by: make.kernel@… Owned by: nobody
Component: Migrations Version:
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have project with two applications - customer and vaucher, here is a vaucher.Vaucher model code i have

class Vaucher(models.Model):
    issuer = models.ForeignKey(User, related_name='vauchers')
    amount = models.DecimalField(max_digits=6, decimal_places=2, default=0)
    created = models.DateTimeField(auto_now=True, auto_now_add=True)
    applied = models.DateTimeField(auto_now=True, auto_now_add=True)
    agreement = models.ForeignKey('customer.Agreement', related_name='vauchers')

it produces following migration

class Migration(migrations.Migration):

    dependencies = [
        ('customer', '__first__'),

    operations = [
                ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)),
                ('issuer', models.ForeignKey(to=settings.AUTH_USER_MODEL, to_field='id')),
                ('amount', models.DecimalField(default=0, max_digits=6, decimal_places=2)),
                ('created', models.DateTimeField(auto_now=True, auto_now_add=True)),
                ('applied', models.DateTimeField(auto_now=True, auto_now_add=True)),
                ('agreement', models.ForeignKey(to='customer.Agreement', to_field='id')),

With this migration ./ migrate gives me following error:

  Applying vaucher.0001_initial...Traceback (most recent call last):
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/apps/", line 152, in get_model
    return self.models[model_name.lower()]
KeyError: 'agreement'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/db/migrations/", line 76, in render
    model = self.apps.get_model(lookup_model[0], lookup_model[1])
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/apps/", line 190, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/apps/", line 155, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'customer' doesn't have a 'agreement' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./", line 10, in <module>
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/core/management/", line 427, in execute_from_command_line
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/core/management/", line 419, in execute
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/core/management/commands/", line 146, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/db/migrations/", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/db/migrations/", line 90, in apply_migration
    if self.detect_soft_applied(migration):
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/db/migrations/", line 134, in detect_soft_applied
    apps = project_state.render()
  File "/home/yuriy/dev/python/tools/django-stable-1.7.x/django/db/migrations/", line 86, in render
ValueError: Lookup failed for model referenced by field vaucher.Vaucher.agreement: customer.Agreement

because automatically created dependency ('customer', '__first__') is wrong, it should be ('customer', '0002_agreement') where Agreement model actually created.

When I change migration file manually it works fine.
When I remove quotes and put required import into agreement = models.ForeignKey(customer.Agreement, related_name='vauchers') makemigrations generates correct dependency and it works fine.

Note - I'm using path from #22659 because of initial data loading fix provided at

Attachments (1)

example.tar.gz (7.9 KB) - added by make.kernel@… 9 years ago.
Test project attached. To reproduce error just run ./ migrate . See my comments in vaucher/migrations/

Download all attachments as: .zip

Change History (2)

Changed 9 years ago by make.kernel@…

Attachment: example.tar.gz added

Test project attached. To reproduce error just run ./ migrate . See my comments in vaucher/migrations/

comment:1 Changed 9 years ago by Andrew Godwin <andrew@…>

Resolution: fixed
Status: newclosed

In 31fc34e447137631e6ea58fc33f3642e65479472:

[1.7.x] Rewrote migration autodetector to involve actual computer science.

Fixes #22605, #22735; also lays the ground for some other fixes.



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