Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#22847 closed Bug (fixed)

Squashmigrations fails with ForeignKey

Reported by: Vidir Valberg Gudmundsson 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

Starting with these models:

class Foo(models.Model):
    pass

class Bar(models.Model):
    text = models.CharField(max_length=255)

which has this migrations:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Bar',
            fields=[
                ('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)),
                ('text', models.CharField(max_length=255)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Foo',
            fields=[
                ('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]

Adding a ForeignKey like this:

class Foo(models.Model):
    pass

class Bar(models.Model):
    text = models.CharField(max_length=255)
    foo = models.ForeignKey('testapp.Foo', null=True)

which results in this migration:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('testapp', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='bar',
            name='foo',
            field=models.ForeignKey(to_field='id', null=True, to='testapp.Foo'),
            preserve_default=True,
        ),
    ]

When trying to squash those, the following error occurs:

$ ./manage.py squashmigrations testapp 0002
Will squash the following migrations:
 - 0001_initial
 - 0002_bar_foo
Do you wish to proceed? [yN] y
Optimizing...
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/core/management/commands/squashmigrations.py", line 79, in handle
    new_operations = optimizer.optimize(operations, migration.app_label)
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/db/migrations/optimizer.py", line 40, in optimize
    result = self.optimize_inner(operations, app_label)
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/db/migrations/optimizer.py", line 54, in optimize_inner
    result = self.reduce(operation, other, operations[i + 1:i + j + 1])
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/db/migrations/optimizer.py", line 159, in reduce
    return om(operation, other, in_between or [])
  File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-packages/django/db/migrations/optimizer.py", line 209, in reduce_create_model_add_field
    other.field.rel.to._meta.object_name,
AttributeError: 'str' object has no attribute '_meta'

Change History (3)

comment:1 by Tim Graham, 11 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Version: master1.7-beta-2

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

Resolution: fixed
Status: newclosed

In 6e3ac5f4743b75c3e3dd36560c92af15e5b72997:

Fixed #22847: Optimizer wasn't expecting unresolved FKs

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

In 02860e779c7a93e50817df55d8a7f64b642319eb:

[1.7.x] Fixed #22847: Optimizer wasn't expecting unresolved FKs

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