Handling circular references in recursive foreign keys

163 views
Skip to first unread message

Jorge M.

unread,
Oct 1, 2014, 5:38:32 PM10/1/14
to django...@googlegroups.com

Django users:

I'm currently trying to deal with something that works just fine in PSQL but causes django some headaches. I have a model like this:

class Application(models.Model):
    ... #other stuff
    paired_with_app = models.ForeignKey(
        'Application',
        blank=True,
        null=True,
    )

Everything works great in SQL and in django until I get two rows that use this foreign key to refer to each other. When this happens, any query involving a row involved in a circular reference takes 6 seconds longer and spits out the python "avoid infinite recursion" error message,

"Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored"

How do I get django to tolerate the circular reference without trying to follow it ad infinitum? All of the code we use it for doesn't follow any references, which means that we're currently setting this foreign key field to the IntegerField "paired_with_app_id" as a workaround.

Thanks,

--

Jorge

Collin Anderson

unread,
Oct 2, 2014, 1:34:29 PM10/2/14
to django...@googlegroups.com
Interesting. It shouldn't be following it at all unless it's using select_related, and even then it shouldn't use select_related for this by default because it's a nullable foreignkey. Or, are you doing some interesting code in your __init__ method?

I just tried creating a circular reference in my project and it came out of the database fine. Is there anyway you can get the top of the maximum recursion depth traceback?
Reply all
Reply to author
Forward
0 new messages