[Django] #36466: Relax check E042 preventing including stored generated fields in composite primary keys

19 views
Skip to first unread message

Django

unread,
Jun 16, 2025, 7:10:14 PM6/16/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Type:
| Cleanup/optimization
Status: new | Component:
| Uncategorized
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Currently E042 prevents including `GeneratedField` in a
`CompositePrimaryKey`.

Postgres & MySQL are both fine with including stored generated columns in
primary keys, including when foreign keys reference these pks. Postgres
(18) & MySQL both reject the inclusion of virtual generated columns.

SQLite doesn't allow generated columns altogether:
https://www.sqlite.org/gencol.html#limitations

I don't have an Oracle instance to test with.

Django should not get in the way of a valid database operations for
vendors that do support a feature. I realise that `--skip-checks` can be
passed in but then we'd be bypassing other useful checks for the lifetime
of a codebase.

Personally I believe we should remove the generated columns part of E042
altogether as the user will find out the mistake soon enough when the
database complains during migration.

Another option may be to just only check for virtual generated columns.

Forum thread: https://forum.djangoproject.com/t/can-we-relax-the-
restriction-e042-on-including-generated-fields-in-composite-pks/41438
--
Ticket URL: <https://code.djangoproject.com/ticket/36466>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 16, 2025, 9:27:28 PM6/16/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* component: Uncategorized => Database layer (models, ORM)

--
Ticket URL: <https://code.djangoproject.com/ticket/36466#comment:1>

Django

unread,
Jun 16, 2025, 11:59:03 PM6/16/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

Comment:

Accepting on the basis that we should do something here.

Whether we should

1. Remove the check altogether for `GeneratedField`
2. Remove the check for `GeneratedField(db_persist=False)`
3. Add a feature flag `supports_stored_generated_field_pk` and base check
of it

is up for debate but given we have a whole section on _Database
limitations_ in the `GeneratedField` docs it seems like 1. is the option
that makes the most sense.
--
Ticket URL: <https://code.djangoproject.com/ticket/36466#comment:2>

Django

unread,
Jun 17, 2025, 2:04:02 AM6/17/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by David Sanders):

For 2 I think you mean `db_persist=True` ;)

But yeah I just noticed that option 1. actually is inline with this:

```
pk = GeneratedField(pk=True, ...)
```

There's no check for this but it will work as long as `db_persist=True`
for mysql & pg, but will fail for sqlite.
--
Ticket URL: <https://code.djangoproject.com/ticket/36466#comment:3>

Django

unread,
Jun 17, 2025, 2:14:19 AM6/17/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* has_patch: 0 => 1

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

Django

unread,
Jun 17, 2025, 4:24:36 AM6/17/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* owner: (none) => David Sanders
* status: new => assigned

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

Django

unread,
Jun 17, 2025, 10:58:20 AM6/17/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_docs: 0 => 1

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

Django

unread,
Jun 18, 2025, 3:49:40 AM6/18/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_docs: 1 => 0

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

Django

unread,
Jun 18, 2025, 5:26:33 AM6/18/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

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

Django

unread,
Jun 18, 2025, 5:26:46 AM6/18/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* version: dev => 5.2

--
Ticket URL: <https://code.djangoproject.com/ticket/36466#comment:9>

Django

unread,
Jun 18, 2025, 11:36:44 AM6/18/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

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

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

Django

unread,
Jun 20, 2025, 2:55:06 AM6/20/25
to django-...@googlegroups.com
#36466: Relax check E042 preventing including stored generated fields in composite
primary keys
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* version: 5.2 => dev

Comment:

Given #36472, the restriction (while can be harsh) does make some sense
and can be relaxed once #36472 is resolved. Hence, I don't plan to
backport to 5.2
--
Ticket URL: <https://code.djangoproject.com/ticket/36466#comment:11>
Reply all
Reply to author
Forward
0 new messages