﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25271	model object locking with select_related	Vathsala Achar	nobody	"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."	Bug	closed	Database layer (models, ORM)	1.8	Normal	invalid	models, select_related, resource locking		Unreviewed	0	0	0	0	0	0
