Django hangs up when select_related() on cross-related models
|Reported by:||Brut[all]||Owned by:||Adrian Holovaty|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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:
wife = models.ForeignKey('woman') # or OneToOneField - for some reason ForeignKey fits here better for me
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 :-)