﻿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
3623	select_related(depth=n) for low n gives corrupt results	jvoung@…	nobody	"Hi, 

I've found that in some cases select_related(depth=n) returns different results from select_related.

Here's a slice of my model:

----
class Lval(models.Model):[[BR]]
    var_id = models.IntegerField(null=True, db_index=True)[[BR]]
    printed = models.CharField(maxlength=100, core=True)[[BR]]
    rep_size = models.IntegerField(null=True)[[BR]]
    declared_at = models.ForeignKey(Program_point, null=True)[[BR]]


class Access(models.Model):[[BR]]
    lval = models.ForeignKey(Lval, related_name=""reads_writes"")[[BR]]
    accessed_through = models.ForeignKey(Call_path)[[BR]]
    occurs_at = models.ForeignKey(Program_point)[[BR]]
    locks = models.ManyToManyField(Lval, filter_interface=models.VERTICAL)[[BR]]


class Race(models.Model):[[BR]]
    access1 = models.ForeignKey(Access, core=True, related_name=""racy1"")[[BR]]
    access2 = models.ForeignKey(Access, core=True, related_name=""racy2"")[[BR]]
    note = models.ForeignKey(Note, core=True, null=True)[[BR]]
    labels = models.ManyToManyField(Label, filter_interface=models.VERTICAL)[[BR]]


class Race_cluster(models.Model):[[BR]]
    races = models.ManyToManyField(Race, filter_interface=models.VERTICAL)[[BR]]
    run = models.ForeignKey(Run)[[BR]]
    def program(self):[[BR]]
        return str(self.run.code)[[BR]]
    def first_race(self):[[BR]]
        return str(self.races.all()[0])[[BR]]


----

when i run the following code:

r = Run.objects.get(id=27)[[BR]]
this_run = Race_cluster.objects.filter(run = r)[[BR]]
for rc in this_run:[[BR]]
   races = rc.races.select_related()[[BR]]
   lval = races[0].access2.lval[[BR]]
   print lval.var_id[[BR]]

I get:

320931
324744
365766
440030
426104
282968
19893
45442 ...

(which is fine...)

but if I change it to select_related(depth=2)

I get:

fs/dcache.c
fs/bio.c
fs/proc/generic.c
include/asm/processor.h
mm/slab.c
fs/block_dev.c
net/rxrpc/transport.c
net/ipv4/igmp.c
lib/kobject.c
kernel/auditsc.c
fs/nfs/nfs4state.c ...

which is weird...

depth=3 gives me:

296
297
298
281
301
302
309
310
315
316 ...

which is also different

finally, depth=4 gives

320931
324744
365766
440030
426104
282968
19893
45442 ...

which is the same as an unbounded select_related().

Also, if I change one line from:[[BR]]

lval = races[0].access2.lval[[BR]]

to[[BR]]

lval = races[0].access1.lval[[BR]]


it works fine w/ depth=2, etc.

I'm not sure if it was meant to work with low depths, but it seems like it should still work. The problem might be some funkyness w/ possibly null foreign keys in my model? BTW, this is running on top of MySQL. Let me know if you need more info. Thanks.

Jan Voung"		closed	Database layer (models, ORM)	dev		duplicate	select_related, depth, qs-rf	gabor@…	Accepted	0	0	0	0	0	0
