Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Django] #36389: GenericRelation uses read connection for writes

12 views
Skip to first unread message

Django

unread,
May 13, 2025, 11:39:49 AMMay 13
to django-...@googlegroups.com
#36389: GenericRelation uses read connection for writes
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Type:
| Uncategorized
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: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When trying to modify a `GenericRelation` (the other side of a
`GenericForeignKey`) in a multi-DB environment the read connection
("replica") is used:

{{{#!python
instance.relation.db # replica - CORRECT

instance.relation.update(field="value") # also uses the replica - WRONG
}}}

This includes when explicitly requesting the "default" DB:

{{{#!python
instance = MyModel.objects.using("default").first()

instance.relation.update(field="value") # also uses the replica - WRONG
}}}

In cases where the `db_for_read` is read-only (common in HA environments),
this produces an error.

I've created a minimal reproduction with a failing test to demo:
https://github.com/RealOrangeOne/django-generic-relation-db-repro. I've
tested this against Django 4.2 - 5.2.

I did some digging, and it seems to be something around the `db` property.
`Model.objects.update` seems to correctly switch to the write instance,
but I can't quite see where. My guess is that the generic relation needs
to do the same, but the inheritance and dynamic classes makes it hard to
trace.
--
Ticket URL: <https://code.djangoproject.com/ticket/36389>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 14, 2025, 3:11:21 AMMay 14
to django-...@googlegroups.com
#36389: GenericRelation uses read connection for writes
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(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 Sarah Boyce):

* stage: Unreviewed => Accepted
* type: Uncategorized => Bug

Comment:

Thank you for the test project!
--
Ticket URL: <https://code.djangoproject.com/ticket/36389#comment:1>

Django

unread,
May 14, 2025, 8:57:32 AMMay 14
to django-...@googlegroups.com
#36389: GenericRelation uses read connection for writes
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(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 Simon Charette):

Slightly related to #15610.
--
Ticket URL: <https://code.djangoproject.com/ticket/36389#comment:2>

Django

unread,
May 16, 2025, 2:16:16 PMMay 16
to django-...@googlegroups.com
#36389: GenericRelation uses read connection for writes
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(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 JaeHyuckSa):

* owner: (none) => JaeHyuckSa
* status: new => assigned

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

Django

unread,
May 16, 2025, 2:19:47 PMMay 16
to django-...@googlegroups.com
#36389: GenericRelation uses read connection for writes
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner:
| JaeHyuckSa
Type: Bug | 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: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by JaeHyuckSa):

* has_patch: 0 => 1

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

Django

unread,
May 19, 2025, 9:48:36 AMMay 19
to django-...@googlegroups.com
#36389: GenericRelation uses read connection for writes
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner:
| JaeHyuckSa
Type: Bug | 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

--
Ticket URL: <https://code.djangoproject.com/ticket/36389#comment:5>
Reply all
Reply to author
Forward
0 new messages