get_or_create defaults to _for_write even when it's just reading
|Reported by:||Rick van Hattem <Rick.van.Hattem@…>||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
First a little background information. I am currently using an asynchronous master slave(s) setup which requires me to pin a request to the master as soon as Django does any writes.
This results in all code reading from the slaves when possible, and switching to the master as soon as data is written.
The problem comes with the get_or_create method. Most of the code that uses get_or_create expects to get a result and uses the create as a fallback if it doesn't exist. The result of this is that (atleast in all the cases where we use get_or_create), the get_or_create method is 99% reads and only a tiny bit of writes. With the current code however, Django defaults to using the write database as soon as get_or_create is called before ever executing an insert.
The link to the line that is causing this:
Was this done by design? If so, please explain the reasoning behind it.
Otherwise I would classify this as a bug and in that case it would be a good idea to move the self._for_write = True line to the except self.model.DoesNotExist: block where the actual writes happen.
Change History (22)
comment:1 Changed 5 years ago by carljm
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
Changed 4 years ago by noria
comment:7 Changed 4 years ago by noria
- Has patch set
comment:13 Changed 4 years ago by Carl Meyer <carl@…>
- Resolution set to fixed
- Status changed from new to closed
comment:15 Changed 4 years ago by carljm
- Resolution fixed deleted
- Status changed from closed to reopened
comment:16 follow-up: ↓ 17 Changed 4 years ago by carljm
- Resolution set to wontfix
- Status changed from reopened to closed