#3045 closed defect (duplicate)
Django hangs up when select_related() on cross-related models
Reported by: | Brut[all] | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | select_related cross-relationships |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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 :-)
Change History (4)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Oh, problem is slightly more complicated.
In example above everything is ok.
Try this:
... and now:
... 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:
... 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).