Opened 17 years ago
Closed 17 years ago
#6571 closed (duplicate)
select_related(depth=1) selects one level more than it should
Reported by: | moep | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
Foo.objetcs.select_related(depth=1)
from django.db import connection
print connection.queries[-1]sql
.... FROM foo, bar, foobar ....
Foo.objetcs.select_related(depth=1)
from django.db import connection
print connection.queries[-1]sql
.... FROM foo, bar ....
Attachments (1)
Change History (4)
comment:1 by , 17 years ago
by , 17 years ago
Attachment: | fill_table_cache.patch added |
---|
comment:2 by , 17 years ago
Has patch: | set |
---|
comment:3 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Dupe of #6018, amongst many others.
Forgot that we have a wiki here