#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.