Django

Code

Ticket #3045 (closed: duplicate)

Opened 2 years ago

Last modified 1 year ago

Django hangs up when select_related() on cross-related models

Reported by: Brut[all] Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords: select_related cross-relationships
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Sometimes we need to relate X to Y and Y to X simultaneously. For example if objects Y belongs to objects X and each X has a "main" Y (a man has fifteen women, but only one is his wife).

We could write something like this:

class man(models.Model):

wife = models.ForeignKey?('woman') # or OneToOneField? - for some reason ForeignKey? fits here better for me

class woman(models.Model):

man = models.ForeignKey?(man)

, but if we then select_related() women, Django hangs on creating SQL query. It tries (as I think) to join men to women, then women to joined men, men to women again and so on - it loops infinitively.

I think, there should be a field option, which will tell Django to no select_related() in this direction. Something like stop_select_related=True - False in default, of course. And Django should look for cross-relationships before it starts making a query in select_related() - to throw a exception instead of simply hangs up :-)

Sorry for my poor english :-)

Attachments

Change History

11/20/06 04:38:25 changed by Brut[all]

Oh, problem is slightly more complicated. In example above everything is ok.

Try this:

class X(models.Model):
    y = models.ForeignKey('Y')

class Y(models.Model):
    x1 = models.ForeignKey(X, related_name="y2")
    x2 = models.ManyToManyField(X, related_name="y3")

... and now:

>>> from avatar.models import X
>>> X.objects.all()
[]
>>> X.objects.select_related()

... and we just created a infinite recursion...

In addition, there is probably a bug somewhere in exception handling in db wrapper, in contrib.admin or maybe in development server? I don't know. I told recently, that Django hangs up, but we saw, that it throw runtime exception only. This is because I recently executed my example in auto-admin site - if we'll add Admin class to model X:

    class Admin:
        list_select_related = True

... and we'll try to get a list of objects X, we'll hangs up server (I tested it only on development server borrowed with Django).

01/20/07 17:43:14 changed by mir@noris.de

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

I'm closing this in favour of #3288. Both have similar conditions, and the tracebacks look the same. #3288 is easier to understand and thus probably better suited to hunt the bug.

09/16/07 08:54:14 changed by PhiR

dup of #2549 too. #3288 is a specific case (X with FK to X).

10/15/07 00:51:20 changed by mtredinnick

(In [6521]) queryset-refactor: Fixed a possibility of shooting oneself in the foot and creating infinite recursion with select_related(). Refs #3045, #3288.


Add/Change #3045 (Django hangs up when select_related() on cross-related models)




Change Properties
Action