Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#21236 closed Bug (fixed)

Migrations: error applying unique_together

Reported by: hjwp Owned by: Tim Graham <timograham@…>
Component: Migrations Version: master
Severity: Normal Keywords:
Cc: tuxcanfly Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by charettes)

This occurs when trying to apply a migration that contains a single modification: the application of a unique_together constraint. full traceback at the end, but the problem seems to come around here:

    self.create_model(temp_model)
  File "/workspace/virtualenv/lib/python3.3/site-packages/django/db/backends/schema.py", line 225, in create_model
    columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
  File "/workspace/virtualenv/lib/python3.3/site-packages/django/db/models/options.py", line 387, in get_field_by_name
    % (self.object_name, name))
django.db.models.fields.FieldDoesNotExist: Item has no field named 'l'

Context: this is a model whose unique_together constraint is on its only two fields, which in this case are called list and text:

        migrations.AlterUniqueTogether(
            unique_together = set(['text', 'list']),
            name = 'item',
        ),

I did a little digging. Inside the 'schema.py', in the create_model function, a little debug print like this:

 print('unique_together', model._meta.unique_together)

gives

unique_together [('l', 'i', 's', 't'), ('t', 'e', 'x', 't')]

Looks like some list comprehension somewhere has gone wrong?

Minimal repro:
Just checkout this repo

https://github.com/hjwp/django-migrations-unique_together-bug-minimal-repro

make sure you're running the latest dev django. there's a requirements.txt in there.

then just cd into myproject, run python manage.py syncdb, and you should get:

$ python manage.py syncdb
Operations to perform:
  Synchronize unmigrated apps: sessions, admin, messages, auth, staticfiles, contenttypes
  Apply all migrations: myapp
Synchronizing apps without migrations:
  Installing custom SQL...
  Installing indexes...
Installed 0 object(s) from 0 fixture(s)
Running migrations:
  Applying myapp.0002_auto...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 397, in execute_from_command_line
    utility.execute()
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 390, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 289, in execute
    output = self.handle(*args, **options)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 419, in handle
    return self.handle_noargs(**options)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 22, in handle_noargs
    call_command("migrate", **options)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 289, in execute
    output = self.handle(*args, **options)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 124, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 89, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 92, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 126, in database_forwards
    getattr(new_model._meta, "unique_together", set()),
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 131, in alter_unique_together
    self._remake_table(model, override_uniques=new_unique_together)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 55, in _remake_table
    self.create_model(temp_model)
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 223, in create_model
    columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
  File "/tmp/django-migrations-unique_together-bug-minimal-repro/venv/local/lib/python2.7/site-packages/django/db/models/options.py", line 387, in get_field_by_name
    % (self.object_name, name))
django.db.models.fields.FieldDoesNotExist: Item has no field named 't'

Change History (13)

comment:1 Changed 20 months ago by hjwp

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Sorry about the double-pasteage of the traceback. I should add -- the problem only occurs if you try and apply the unique_together migration on its own. If you make a single migration that also creates the models, then migrate applies with no problems...

comment:2 Changed 20 months ago by charettes

  • Description modified (diff)

comment:3 Changed 20 months ago by hjwp

@charettes - thanks! if you feel like correcting a couple of other lazy typos, " in this case are called list and item:" should read " in this case are called list and text:", and also, the second traceback, listed after the words "full trac", is unnecessary (it just dupes the first one).

comment:4 Changed 20 months ago by charettes

  • Description modified (diff)

I'm not that familiar with the migration infrastructure yet but did you create that migrations.AlterUniqueTogether instance yourself or was it generated by django?

comment:5 Changed 20 months ago by hjwp

@charrettes, it's the migration that's generated automatically by the tool. I didn't roll it myself.

comment:6 Changed 20 months ago by charettes

  • Triage Stage changed from Unreviewed to Accepted

comment:7 Changed 20 months ago by tuxcanfly

  • Triage Stage changed from Accepted to Unreviewed

I suspect this is because the makemigration command doesn't handle the convenience case where unique_together is a single tuple instead of the usual tuple of tuples.

comment:8 Changed 20 months ago by tuxcanfly

  • Triage Stage changed from Unreviewed to Accepted

comment:9 Changed 20 months ago by tuxcanfly

  • Cc tuxcanfly added
  • Has patch set
  • Needs tests set

Here's a patch that appears to fix the migration error.

https://github.com/django/django/pull/1716

comment:10 Changed 20 months ago by tuxcanfly

  • Needs tests unset

comment:11 Changed 20 months ago by Tim Graham <timograham@…>

  • Owner set to Tim Graham <timograham@…>
  • Resolution set to fixed
  • Status changed from new to closed

In 4dbd95ad65fa3fc86af7391b28a759b401f530d1:

Fixed #21236 -- Allowed migrations to work with unique_together tuples.

Thanks hjwp for the report.

comment:12 Changed 20 months ago by anonymous

I'm bloody impressed with the turnaround! Nice one gang.

comment:13 Changed 20 months ago by tuxcanfly

Thanks to hjwp for the detailed bug report and Tim Graham for reviewing and merging the patch!

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