Re: [Django] #34333: Migrations tries to add constraint before adding a foreign key.

38 views
Skip to first unread message

Django

unread,
Feb 13, 2023, 11:51:15 PM2/13/23
to django-...@googlegroups.com
#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.

Django

unread,
Feb 14, 2023, 5:16:50 AM2/14/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Sota Tabu
Type: Bug | Status: assigned

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
-------------------------------------+-------------------------------------
Changes (by Sota Tabu):

* owner: nobody => Sota Tabu
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:5>

Django

unread,
Mar 7, 2023, 2:25:51 PM3/7/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Durval
| Carvalho

Type: Bug | Status: assigned
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
-------------------------------------+-------------------------------------
Changes (by Durval Carvalho):

* owner: Sota Tabu => Durval Carvalho


--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:6>

Django

unread,
Mar 7, 2023, 3:24:04 PM3/7/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Durval
| Carvalho
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migration, | Triage Stage: Accepted
constraint, field |
Has patch: 0 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Durval Carvalho):

* cc: Durval Carvalho (added)
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:7>

Django

unread,
Mar 9, 2023, 2:04:38 PM3/9/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Durval
| Carvalho
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migration, | Triage Stage: Accepted
constraint, field |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1
* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:8>

Django

unread,
Mar 9, 2023, 5:00:05 PM3/9/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Durval
| Carvalho
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migration, | Triage Stage: Accepted
constraint, field |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Wobrock):

* 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>

Django

unread,
Mar 10, 2023, 1:06:26 AM3/10/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Durval
| Carvalho
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migration, | Triage Stage: Ready for
constraint, field | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34333#comment:10>

Django

unread,
Mar 10, 2023, 2:35:42 AM3/10/23
to django-...@googlegroups.com
#34333: Migrations tries to add constraint before adding a foreign key.
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: Durval
| Carvalho
Type: Bug | Status: closed
Component: Migrations | Version: 4.1
Severity: Normal | Resolution: fixed

Keywords: migration, | Triage Stage: Ready for
constraint, field | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages