[Django] #36438: makemigrations removes fields in wrong order when GeneratedFields are involved

15 views
Skip to first unread message

Django

unread,
Jun 4, 2025, 11:38:05 AMJun 4
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Type:
| Cleanup/optimization
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Consider a model like PayItem with fields quantity, rate and total.

When total is a GeneratedField dependent on quantity, if you remove
quantity and total and generate migrations you get a migration like


{{{
operations = [
migrations.RemoveField(
model_name="payitem",
name="total",
),
migrations.RemoveField(
model_name="payitem",
name="quantity",
),
]
}}}

which will throw django.db.utils.OperationalError: (3108, "Column 'amount'
has a generated column dependency.").

It'd be nice if makemigrations listed operations in the correct order.
--
Ticket URL: <https://code.djangoproject.com/ticket/36438>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 4, 2025, 11:42:20 AMJun 4
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: csaska | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by csaska):

Replying to [ticket:36438 csaska]:
> Consider a model like PayItem with fields quantity, rate and total.
>
> When total is a GeneratedField dependent on quantity, if you remove
quantity and total and generate migrations you get a migration like
>
>
> {{{
> operations = [
> migrations.RemoveField(
> model_name="payitem",
> name="total",
> ),
> migrations.RemoveField(
> model_name="payitem",
> name="quantity",
> ),
> ]
> }}}
>
> which will throw django.db.utils.OperationalError: (3108, "Column
'amount' has a generated column dependency.").
>
> It'd be nice if makemigrations listed operations in the correct order.

In my example, I actually had fields amount, rate and total. Perhaps it
using alphabetical order and thus dropping amount operation comes before
dropping total operation?
--
Ticket URL: <https://code.djangoproject.com/ticket/36438#comment:1>

Django

unread,
Jun 4, 2025, 11:57:05 AMJun 4
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Migrations | Version: 5.2
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage: Accepted
removefield |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* component: Database layer (models, ORM) => Migrations
* easy: 1 => 0
* keywords: => generatedfield removefield
* stage: Unreviewed => Accepted

Comment:

I haven't reproduced myself but given I can't find any logic in
`AutoDetector._generate_removed_field` to ensure that `GeneratedField`
removals depend on removals of fields they reference this seems legitimate
--
Ticket URL: <https://code.djangoproject.com/ticket/36438#comment:2>

Django

unread,
Jun 4, 2025, 3:23:12 PMJun 4
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Migrations | Version: 5.2
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage: Accepted
removefield |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* owner: (none) => Clifford Gama
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36438#comment:3>

Django

unread,
Jul 6, 2025, 2:50:10 AMJul 6
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Migrations | Version: 5.2
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage: Accepted
removefield |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36438#comment:4>

Django

unread,
Jul 24, 2025, 6:17:40 AMJul 24
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Migrations | Version: 5.2
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage: Accepted
removefield |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_tests: 0 => 1

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

Django

unread,
Jul 25, 2025, 3:46:27 AMJul 25
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Migrations | Version: 5.2
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage: Ready for
removefield | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

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

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

Django

unread,
Jul 25, 2025, 7:59:24 AMJul 25
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Migrations | Version: 5.2
Severity: Normal | Resolution: fixed
Keywords: generatedfield | Triage Stage: Ready for
removefield | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"1a7fc0f65d5b2ef56625a8b5897496e28a5834ff" 1a7fc0f]:
{{{#!CommitTicketReference repository=""
revision="1a7fc0f65d5b2ef56625a8b5897496e28a5834ff"
Fixed #36438 -- Made MigrationAutodetector remove generated fields before
their base fields.

Thanks to Colton Saska for the report and to Simon Charette for the
review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36438#comment:7>

Django

unread,
Jul 25, 2025, 7:59:25 AMJul 25
to django-...@googlegroups.com
#36438: makemigrations removes fields in wrong order when GeneratedFields are
involved
-------------------------------------+-------------------------------------
Reporter: Colton Saska | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Migrations | Version: 5.2
Severity: Normal | Resolution: fixed
Keywords: generatedfield | Triage Stage: Ready for
removefield | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"45ba7683a6e399815731a13cceb3acb518a2f1f2" 45ba7683]:
{{{#!CommitTicketReference repository=""
revision="45ba7683a6e399815731a13cceb3acb518a2f1f2"
Refs #36438 -- Made FieldOperation.references_field() detect references in
GeneratedField.expression.

Thanks to Simon Charette for the suggestion and review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36438#comment:8>
Reply all
Reply to author
Forward
0 new messages