Opened 17 years ago

Closed 17 years ago

Last modified 16 years ago

#3623 closed (duplicate)

select_related(depth=n) for low n gives corrupt results

Reported by: jvoung@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: select_related, depth, qs-rf
Cc: gabor@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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):

var_id = models.IntegerField(null=True, db_index=True)
printed = models.CharField(maxlength=100, core=True)
rep_size = models.IntegerField(null=True)
declared_at = models.ForeignKey(Program_point, null=True)

class Access(models.Model):

lval = models.ForeignKey(Lval, related_name="reads_writes")
accessed_through = models.ForeignKey(Call_path)
occurs_at = models.ForeignKey(Program_point)
locks = models.ManyToManyField(Lval, filter_interface=models.VERTICAL)

class Race(models.Model):

access1 = models.ForeignKey(Access, core=True, related_name="racy1")
access2 = models.ForeignKey(Access, core=True, related_name="racy2")
note = models.ForeignKey(Note, core=True, null=True)
labels = models.ManyToManyField(Label, filter_interface=models.VERTICAL)

class Race_cluster(models.Model):

races = models.ManyToManyField(Race, filter_interface=models.VERTICAL)
run = models.ForeignKey(Run)
def program(self):

return str(self.run.code)

def first_race(self):

return str(self.races.all()[0])


when i run the following code:

r = Run.objects.get(id=27)
this_run = Race_cluster.objects.filter(run = r)
for rc in this_run:

races = rc.races.select_related()
lval = races[0].access2.lval
print lval.var_id

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:

lval = races[0].access2.lval

to

lval = races[0].access1.lval

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

Change History (7)

comment:1 by Gary Wilson <gary.wilson@…>, 17 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Gary Wilson <gary.wilson@…>, 17 years ago

also, could an admin please fix the code formatting in the ticket description, thanks.

comment:3 by boris.erdmann@…, 17 years ago

Please Look at ticket #3275 comment 03/04/07 02:12:37 and
http://groups.google.com/group/django-developers/browse_thread/thread/400bcdee7fedbb15#
My comment from 2007-03-03 14:30

The quoted posts explain, what I changed to get it working for me.
Feedback appreciated.

comment:4 by Gábor Farkas <gabor@…>, 17 years ago

Cc: gabor@… added

comment:5 by Malcolm Tredinnick, 17 years ago

Keywords: qs-rf added

comment:6 by Philippe Raoult, 17 years ago

Resolution: duplicate
Status: newclosed

duplicate of #3275 (#4789 too).

comment:7 by Matthias Urlichs <smurf@…>, 16 years ago

Unfortunately, #3275 has issues beyond fixing this bug, so closing it here was a little premature.

The problem is however discussed further (and, one hopes, fixed) in #6018.

Note: See TracTickets for help on using tickets.
Back to Top