Opened 11 years ago

Last modified 11 years ago

#21236 closed Bug

Migrations: error applying unique_together — at Initial Version

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

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 "item":

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'

Full trac
/home/harry/Dropbox/book/source/chapter_09/virtualenv/lib/python3.3/site-packages/django/db/backends/schema.py

Applying myapp.0002_auto...Traceback (most recent call last):
File "/tmp/repro/venv/lib/python3.3/site-packages/django/db/models/options.py", line 381, in get_field_by_name

return self._name_map[name]

AttributeError: 'Options' object has no attribute '_name_map'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/tmp/repro/venv/lib/python3.3/site-packages/django/db/models/options.py", line 384, in get_field_by_name

return cache[name]

KeyError: 't'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "manage.py", line 10, in <module>

execute_from_command_line(sys.argv)

File "/tmp/repro/venv/lib/python3.3/site-packages/django/core/management/init.py", line 397, in execute_from_command_line

utility.execute()

File "/tmp/repro/venv/lib/python3.3/site-packages/django/core/management/init.py", line 390, in execute

self.fetch_command(subcommand).run_from_argv(self.argv)

File "/tmp/repro/venv/lib/python3.3/site-packages/django/core/management/base.py", line 242, in run_from_argv

self.execute(*args, options.dict)

File "/tmp/repro/venv/lib/python3.3/site-packages/django/core/management/base.py", line 289, in execute

output = self.handle(*args, options)

File "/tmp/repro/venv/lib/python3.3/site-packages/django/core/management/commands/migrate.py", line 124, in handle

executor.migrate(targets, plan, fake=options.get("fake", False))

File "/tmp/repro/venv/lib/python3.3/site-packages/django/db/migrations/executor.py", line 60, in migrate

self.apply_migration(migration, fake=fake)

File "/tmp/repro/venv/lib/python3.3/site-packages/django/db/migrations/executor.py", line 89, in apply_migration

migration.apply(project_state, schema_editor)

File "/tmp/repro/venv/lib/python3.3/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/repro/venv/lib/python3.3/site-packages/django/db/migrations/operations/models.py", line 126, in database_forwards

getattr(new_model._meta, "unique_together", set()),

File "/tmp/repro/venv/lib/python3.3/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/repro/venv/lib/python3.3/site-packages/django/db/backends/sqlite3/schema.py", line 55, in _remake_table

self.create_model(temp_model)

File "/tmp/repro/venv/lib/python3.3/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/repro/venv/lib/python3.3/site-packages/django/db/backends/schema.py", line 223, in <listcomp>

columns = [model._meta.get_field_by_name(field)[0].column for field in fields]

File "/tmp/repro/venv/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 't'

Change History (0)

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