Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#23014 closed Bug (fixed)

Renaming a field in a unique_together fails

Reported by: Stephen Burrows Owned by: Tim Graham
Component: Migrations Version: 1.7-rc-1
Severity: Release blocker Keywords:
Cc: Stephen Burrows, mathieu@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

To reproduce this, set up a model with two fields and a unique_together constraint. Now try to rename one of the fields to something else. You'll get an error like this (at least for SQLite):

  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ".../django/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File ".../django/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".../django/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ".../django/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File ".../django/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File ".../django/django/db/migrations/executor.py", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File ".../django/django/db/migrations/executor.py", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File ".../django/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File ".../django/django/db/migrations/operations/fields.py", line 84, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
  File ".../django/django/db/backends/sqlite3/schema.py", line 158, in remove_field
    self._remake_table(model, delete_fields=[field])
  File ".../django/django/db/backends/sqlite3/schema.py", line 110, in _remake_table
    self.create_model(temp_model)
  File ".../django/django/db/backends/schema.py", line 261, in create_model
    columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
  File ".../django/django/db/models/options.py", line 420, in get_field_by_name
    % (self.object_name, name))
django.db.models.fields.FieldDoesNotExist: OrderDiscount has no field named 'event_person'

The only "workaround" I was able to find is to first add the "new" field, then alter the unique_together constraint, then remove the old field. That works for me since we don't have any data we care about - and if we did, we could just add a data operation - but it seems like this should just work.

Change History (11)

comment:1 Changed 4 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:2 Changed 4 years ago by Andrew Godwin <andrew@…>

Resolution: fixed
Status: newclosed

In e24e9e0438cf3ba6c557558f68587d135a85e126:

Fixed #23014: Renaming not atomic with unique together

comment:3 Changed 4 years ago by Andrew Godwin <andrew@…>

In e2220c1c086675e5c3a87d2f09cb71e6562ad733:

[1.7.x] Fixed #23014: Renaming not atomic with unique together

comment:4 Changed 4 years ago by Tim Graham

Resolution: fixed
Status: closednew

After this fix, I get infinite migrations when running makemigrations for auth & contenttypes.

comment:5 Changed 4 years ago by Maik Hoepfel

Just want to confirm that I'm running into the same issue as timo when using 2c6ef625ad73c38769f086733356e37a938b69c3 from stable/1.7.x. Every run of makemigrations creates a set of migrations that contains an AlterUniqueTogether action.

comment:6 Changed 4 years ago by Mathieu Agopian

Same here, I can confirm.

To reproduce, there's an even easier way:

  • create a new project with "startproject"
  • apply the migrations (auth, contenttype...)
  • run "makemigrations": ./manage.py makemigrations --dry-run

comment:7 Changed 4 years ago by Mathieu Agopian

Cc: mathieu@… added

comment:8 Changed 4 years ago by Tim Graham

Owner: changed from nobody to Tim Graham
Status: newassigned

comment:9 Changed 4 years ago by Tim Graham

Has patch: set

comment:10 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 64e75c47ef8dd3c61b85798c7bb34e48c52ccc96:

Fixed #23014 -- Infinite migration regression with unique/index_together.

comment:11 Changed 4 years ago by Tim Graham <timograham@…>

In b918bc921ca104ac0ab877e9fa0f6b49a5242679:

[1.7.x] Fixed #23014 -- Infinite migration regression with unique/index_together.

Backport of 64e75c47ef from master

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