Comment (by Simon Charette):
For anyone interested in fixing this issue the bug lies in
`django.db.migrations.autodetector.MigrationAutodetector` in each
alteration and addition methods that relate to indexes and constraints and
call `add_operation` without specifying `dependencies`.
If you look at how `_generate_altered_foo_together` is implement it
[https://github.com/django/django/blob/1eb94bc8dab46dfa117d21ef4f3b52aebb593615/django/db/migrations/autodetector.py#L1454-L1466
"does it right"] by building dependencies for foreign keys references in
`(index|unique)_together`. The same needs to be done for indexes and
constraints but since they also support expressions and conditions these
needs to be considered as well.
Basically a set of fields references from `.fields`, `.expressions`,
`.include` and `.condition` has to be extracted and for each `Index` and
`Constraint` and `_get_dependencies_for_foreign_key` has to be called for
each remote fields and combined to be passed as `dependencies` to the
`add_operation` call.
Doing so for `.fields` and `.include` are trivial, for `.expressions` and
`.condition` I would refer to `Expression.flatten` as it's used in
`UniqueConstraint.validate` that might be a good reason to add
`Expression.field_refs` method of simply make `F.get_refs` return
`{self.name}` so `Expression.get_refs` can be used generically (for alias
and field references).
All that to say this is #25551 back from the dead 7 years later with new
APIs.
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Sota Tabu
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:5>
* owner: Sota Tabu => Durval Carvalho
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:6>
* cc: Durval Carvalho (added)
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:7>
* has_patch: 0 => 1
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:8>
* cc: David Wobrock (added)
* needs_better_patch: 0 => 1
Comment:
For the
[https://github.com/django/django/pull/16635#discussion_r1131656461 tiny
things] to fix in the PR. 😉
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:9>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"4b1bfea2846f66f504265cec46ee1fe94ee9c98b" 4b1bfea]:
{{{
#!CommitTicketReference repository=""
revision="4b1bfea2846f66f504265cec46ee1fe94ee9c98b"
Fixed #34333 -- Fixed migration operations ordering when adding
index/constraint on new foreign key.
Thanks Simon Charette and David Wobrock for reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:11>