#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migration, | Triage Stage: Accepted
constraint, field |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701864e432efc-48cce544-80b1-4098-a303-62b885ee36e6-000000%40eu-central-1.amazonses.com.