Opened 6 years ago

Closed 6 years ago

#23859 closed Bug (fixed)

Renaming a field in a index_together fails

Reported by: Markus Holtermann Owned by: Markus Holtermann
Component: Migrations Version: 1.7
Severity: Normal Keywords:
Cc: Markus Holtermann Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This issue is almost the same as #23014.

Renaming a field in a model that is part of an index_together fails with the same error as in the referenced ticket.

Old model:

class Foo(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField(default=42)

    class Meta:
        index_together = (('foo', 'bar'), )

New model:

class Foo(models.Model):
    foo = models.IntegerField()
    bar2 = models.IntegerField(default=42)

    class Meta:
        index_together = (('foo', 'bar2'), )

Traceback:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/markus/Coding/django/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/markus/Coding/django/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/markus/Coding/django/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/markus/Coding/django/django/core/management/base.py", line 442, in execute
    output = self.handle(*args, **options)
  File "/home/markus/Coding/django/django/core/management/commands/migrate.py", line 193, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/markus/Coding/django/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/markus/Coding/django/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/markus/Coding/django/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/markus/Coding/django/django/db/migrations/operations/models.py", line 297, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "/home/markus/Coding/django/django/db/backends/schema.py", line 320, in alter_index_together
    columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
  File "/home/markus/Coding/django/django/db/backends/schema.py", line 320, in <listcomp>
    columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
  File "/home/markus/Coding/django/django/db/models/options.py", line 410, in get_field_by_name
    % (self.object_name, name))
django.db.models.fields.FieldDoesNotExist: Foo has no field named 'bar'

Change History (10)

comment:1 Changed 6 years ago by Anton Baklanov

Triage Stage: UnreviewedAccepted

Reproducible on both stable/1.7.x and master

comment:2 Changed 6 years ago by Markus Holtermann

Has patch: set
Status: newassigned

Thank you for the confirmation, bak1an.

Here's a pull-request: https://github.com/django/django/pull/3563

comment:3 Changed 6 years ago by Markus Holtermann <info@…>

Resolution: fixed
Status: assignedclosed

In 7b4a994599b75a07cb07d1e0cc26b3bbf25ab7a6:

Fixed #23859 -- Fixed a migration crash when a field is renamed that is part of an index_together

comment:4 Changed 6 years ago by Carl Meyer <carl@…>

In 6ae1e3ba9f3a5316504c7543ba4abc472990de95:

Merge pull request #3563 from MarkusH/ticket23859

Fixed #23859 -- Fixed a migration crash when a field is renamed that is part of an index_together

comment:5 Changed 6 years ago by Carl Meyer <carl@…>

In 03d983f7c39e8ba5b09d4000a234768715f8834f:

[1.7.x] Fixed #23859 -- Fixed a migration crash when a field is renamed that is part of an index_together

Backport of 7b4a994599b75a07cb07d1e0cc26b3bbf25ab7a6 from master.

comment:6 Changed 6 years ago by Tim Graham

Has patch: unset
Resolution: fixed
Status: closednew

This is causing a test failure on Oracle:

======================================================================
ERROR: test_rename_field (migrations.test_operations.OperationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/migrations/test_operations.py", line 988, in test_rename_field
    project_state = self.set_up_test_model("test_rnfl", unique_together=True, index_together=True)
  File "/home/tim/code/django/tests/migrations/test_operations.py", line 146, in set_up_test_model
    return self.apply_operations(app_label, ProjectState(), operations)
  File "/home/tim/code/django/tests/migrations/test_operations.py", line 32, in apply_operations
    return migration.apply(project_state, editor)
  File "/home/tim/code/django/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/tim/code/django/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/home/tim/code/django/django/db/backends/schema.py", line 266, in create_model
    self.execute(self._create_index_sql(model, fields, suffix="_idx"))
  File "/home/tim/code/django/django/db/backends/schema.py", line 102, in execute
    cursor.execute(sql, params)
  File "/home/tim/code/django/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/tim/code/django/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/tim/code/django/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/tim/code/django/django/db/backends/oracle/base.py", line 916, in execute
    return self.cursor.execute(query, self._param_generator(params))
DatabaseError: ORA-01408: such column list already indexed

comment:7 Changed 6 years ago by Markus Holtermann

Has patch: set

This issue should be fixed by https://github.com/django/django/pull/3589

comment:8 Changed 6 years ago by Tim Graham <timograham@…>

In 4c709cc0ef0daa2b527e056865f51796fb0d42f3:

Fixed duplicate index error on Oracle; refs #23859.

Refers to regression introduced in 7b4a994599b75a07cb07d1e0cc26b3bbf25ab7a6

comment:9 Changed 6 years ago by Tim Graham <timograham@…>

In 145467a63688eb9055d176d84655cc029060da30:

[1.7.x] Fixed duplicate index error on Oracle; refs #23859.

Refers to regression introduced in 7b4a994599b75a07cb07d1e0cc26b3bbf25ab7a6

Backport of 4c709cc0ef0daa2b527e056865f51796fb0d42f3 from master

comment:10 Changed 6 years ago by Tim Graham

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top