get_or_create defaults to _for_write even when it's just reading
|Reported by:||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 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
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Accepted|