#16865: get_or_create defaults to _for_write even when it's just reading
-------------------------------------+-------------------------------------
 Reporter:  Rick van Hattem          |          Owner:  nobody
  <Rick.van.Hattem@…>                |         Status:  new
     Type:  Bug                      |      Component:  Database layer
Milestone:                           |  (models, ORM)
  Version:  1.3                      |       Severity:  Normal
 Keywords:                           |   Triage Stage:  Unreviewed
Has patch:  0                        |  Easy pickings:  1
    UI/UX:  0                        |
-------------------------------------+-------------------------------------
 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:
 
https://code.djangoproject.com/browser/django/trunk/django/db/models/query.py#L407

 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16865>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to