Opened 9 years ago

Closed 8 years ago

Last modified 8 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: master
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: UI/UX:

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 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 9 years ago by Gary Wilson <gary.wilson@…>

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

comment:3 Changed 8 years ago by boris.erdmann@…

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 Changed 8 years ago by Gábor Farkas <gabor@…>

  • Cc gabor@… added

comment:5 Changed 8 years ago by mtredinnick

  • Keywords qs-rf added

comment:6 Changed 8 years ago by PhiR

  • Resolution set to duplicate
  • Status changed from new to closed

duplicate of #3275 (#4789 too).

comment:7 Changed 8 years ago by Matthias Urlichs <smurf@…>

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