#25551: makemigrations when adding a ForeignKey to a newly created model along
with
a unique_together constraint must create fields before the unique_together
constraint
----------------------------+--------------------
Reporter: mssnlayam | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
When adding a new model and a `ForeignKey` to it as well a
`unique_together` constraint for the `ForeignKey` field, Django migrations
should create the field before applying the `unique_together` constraint.
In the example below, I am adding a new `Author` model, a field to this
model and a `unique_together` constraint for this field.
{{{#!diff
from django.db import models
+class Author(models.Model):
+ name = models.CharField(max_length=255)
+
class Book(models.Model):
name = models.CharField(max_length=255)
+ author = models.ForeignKey(Author, null=True)
+ class Meta:
+ unique_together = (
+ ('name', 'author'),
+ )
}}}
Django's migrations framework produces the following summary output:
{{{
Migrations for 'testapp':
0002_auto_20151013_1354.py:
- Create model Author
- Alter unique_together for book (1 constraint(s))
- Add field author to book
}}}
Here's the relevant portion of the migrations file:
{{{#!python
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.AutoField(verbose_name='ID',
serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=255)),
],
),
migrations.AlterUniqueTogether(
name='book',
unique_together=set([('name', 'author')]),
),
migrations.AddField(
model_name='book',
name='author',
field=models.ForeignKey(to='testapp.Author', null=True),
),
]
}}}
This migration fails with the following stacktrace:
{{{
Operations to perform:
Apply all migrations: testapp
Running migrations:
Rendering model states... DONE
Applying testapp.0002_auto_20151013_1354...Traceback (most recent call
last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 351, in
execute_from_command_line
utility.execute()
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake,
fake_initial=fake_initial)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/migrations/operations/models.py", line 359, in
database_forwards
getattr(new_model._meta, self.option_name, set()),
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/schema.py", line 219, in
alter_unique_together
self._remake_table(model, override_uniques=new_unique_together)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/schema.py", line 140, in _remake_table
self.create_model(temp_model)
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/backends/base/schema.py", line 272, in create_model
columns = [model._meta.get_field(field).column for field in fields]
File "/home/vagrant/testunique-env/local/lib/python2.7/site-
packages/django/db/models/options.py", line 554, in get_field
raise FieldDoesNotExist('%s has no field named %r' %
(self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Book has no field named
u'author'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25551>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.f884231f4c0f9dd1a456178433fe3b5c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.