#25271: model object locking with select_related
-------------------------------------+-------------------------------------
     Reporter:  VathsalaAchar        |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  1.8
  (models, ORM)                      |   Keywords:  models, select_related,
     Severity:  Normal               |  resource locking
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 I used select_related (django 1.6 on windows connected to sqlserver) and
 came across some unexpected behaviour.

 Here's an example that I tested with django 1.8 and sqlite:

 {{{

 class TestUser(models.Model):
     name = models.CharField(blank=True, max_length=255)


 class Account(models.Model):
     user = models.OneToOneField(TestUser)
     name = models.CharField(max_length=255)
     value = models.IntegerField(default=0)
 }}}


 I tried the following code against the models above to replicate the
 unexpected behaviour I noticed.

 {{{
  def some_method(some_id):
      t = TestUser.objects.select_related('account').get(id=some_id)

      # here is where the fun stuff happens
      alter_account(t.id)

      # the following changes are reflected in the db
      t.account.name= 'something else'
      t.account.save()


 def alter_account(uid):
      '''
      this method does not work as expected as the select_related in the
      previous method has locked the Account model out
      '''
      a = Account.objects.get(user__id=uid)
      a.value = 5
      a.save()

 test_user = TestUser.objects.create(name='test user')
 test_account = Account.objects.create(user=test_user, name='some account')
 }}}

 Initial state:
 {{{
 >>> test_account.name
 u'some account'
 >>> test_account.value
 0
 }}}

 Expected:
 {{{
 >>> test_account.name
 u'something else'
 >>> test_account.value
 5
 }}}

 Unexpected result:

 {{{
 >>> some_method(test_user.id)
 >>> test_account.name
 u'something else'
 >>> test_account.value
 0
 }}}

 I found this in our production code when the object state refused to
 update and caused us issues. I had to get rid of the select_related in the
 query to resolve the issue.

 Nowhere in the documentation is this behaviour explained, so I assume this
 is a bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/25271>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/056.c2110a5ae6781236f6c62a842ab80c1c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to