[Django] #25271: model object locking with select_related

5 views
Skip to first unread message

Django

unread,
Aug 13, 2015, 5:50:53 AM8/13/15
to django-...@googlegroups.com
#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.

Django

unread,
Aug 13, 2015, 6:42:20 AM8/13/15
to django-...@googlegroups.com
#25271: model object locking with select_related
-------------------------------------+-------------------------------------
Reporter: VathsalaAchar | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: models, | Triage Stage:
select_related, resource locking | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

This is expected behavior because `select_related()` has already fetch the
related accounts. Since you modify a separate model instance, the
originally fetched instance becomes stale.

--
Ticket URL: <https://code.djangoproject.com/ticket/25271#comment:1>

Reply all
Reply to author
Forward
0 new messages