Opened 11 years ago

Last modified 11 years ago

#21236 closed Bug

Migrations: error applying unique_together — at Version 4

Reported by: Harry Percival Owned by:
Component: Migrations Version: dev
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 Simon Charette)

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 (4)

comment:1 by Harry Percival, 11 years ago

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 by Simon Charette, 11 years ago

Description: modified (diff)

comment:3 by Harry Percival, 11 years ago

@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 by Simon Charette, 11 years ago

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?

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