Django

Code

Ticket #3623 (closed: duplicate)

Opened 1 year ago

Last modified 7 months ago

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

Reported by: jvoung@cs.ucsd.edu Assigned to: nobody
Milestone: Component: Database wrapper
Version: SVN Keywords: select_related, depth, qs-rf
Cc: gabor@nekomancer.net Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Attachments

Change History

03/01/07 19:00:29 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

03/01/07 19:01:48 changed by Gary Wilson <gary.wilson@gmail.com>

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

03/05/07 03:39:59 changed by boris.erdmann@googlemail.com

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.

07/09/07 13:54:27 changed by Gábor Farkas <gabor@nekomancer.net>

  • cc set to gabor@nekomancer.net.

09/13/07 16:39:01 changed by mtredinnick

  • keywords changed from select_related, depth to select_related, depth, qs-rf.

09/14/07 12:04:24 changed by PhiR

  • status changed from new to closed.
  • resolution set to duplicate.

duplicate of #3275 (#4789 too).

11/28/07 02:10:52 changed by Matthias Urlichs <smurf@smurf.noris.de>

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.


Add/Change #3623 (select_related(depth=n) for low n gives corrupt results)




Change Properties
Action