﻿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
6571	select_related(depth=1) selects one level more than it should	moep	nobody	"Hi,

select_related(depth=1) does select one level more than it
should.

The problem is that django.db.models.query.fill_table_cache has cur_depth=0 
by default and checks if max_depth and max_depth > cur_depth. That means that
max_depth has to catch up to cur_depth one round, before it works as expected.

Changing cur_depth to 1 or checking max_depth >= cur_depth would solve
the problem but i don't know whether it introduces any other problems.

Regards


class Foo(models.Model):
	bar = models.ForeignKey(Bar)

class Bar(models.Model):
	foo_bar = models.ForeignKey(FooBar)

class Foobar(models.Model):
	pass

------ cur_depth=0
Foo.objetcs.select_related(depth=1)
from django.db import connection
print connection.queries[-1]['sql']
>>> .... FROM foo, bar, foobar ....

------ cur_depth=1 or max_depth >= cur_depth
Foo.objetcs.select_related(depth=1)
from django.db import connection
print connection.queries[-1]['sql']
>>> .... FROM foo, bar ...."		closed	Database layer (models, ORM)	dev		duplicate			Unreviewed	1	0	0	0	0	0
