Opened 9 days ago

Last modified 8 days ago

#35991 assigned Bug

Migrations crash on SQLite when renaming part of CompositePrimaryKey.

Reported by: Mariusz Felisiak Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: dev
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've created a sample project that tries to rename a column included in CompositePrimaryKey. Unfortunately, it crashes:

$ python manage.py migrate test_one 0002
...
  File "/django/db/backends/base/schema.py", line 509, in create_model
    sql, params = self.table_sql(model)
                  ^^^^^^^^^^^^^^^^^^^^^
  File "/django/db/backends/base/schema.py", line 290, in table_sql
    constraint_sqls.append(self._pk_constraint_sql(pk.columns))
                                                   ^^^^^^^^^^
  File "/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/django/db/models/fields/composite.py", line 81, in columns
    return tuple(field.column for field in self.fields)
                                           ^^^^^^^^^^^
  File "/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/django/db/models/fields/composite.py", line 77, in fields
    return tuple(meta.get_field(field_name) for field_name in self.field_names)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/django/db/models/fields/composite.py", line 77, in <genexpr>
    return tuple(meta.get_field(field_name) for field_name in self.field_names)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/django/db/models/options.py", line 685, in get_field
    raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: NewRelease has no field named 'name'

I've attached a sample project. I can prepare a patch in the coming days.

Attachments (1)

ticket_373_rename_column.zip (7.9 KB ) - added by Mariusz Felisiak 9 days ago.

Download all attachments as: .zip

Change History (4)

by Mariusz Felisiak, 9 days ago

comment:1 by Mariusz Felisiak, 9 days ago

I've found other migrations that crash in exactly the same way on SQLite.

Last edited 9 days ago by Mariusz Felisiak (previous) (diff)

comment:2 by Simon Charette, 8 days ago

Triage Stage: UnreviewedAccepted

Thanks for the report!

The issue likely lies in ProjectState.rename_field and in MigrationAutoDetector.create_renamed_fields which shouldn't generate the AlterField against the composite primary key in the first place.

comment:3 by Mariusz Felisiak, 8 days ago

Owner: set to Mariusz Felisiak
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top