"maximum recursion depth exceeded" occurred in admin when deleting #31

Closed
wong2 opened this Issue Apr 6, 2012 · 5 comments

Projects

None yet

2 participants

@wong2

As I said in issue #29, I saw this error when I open the model with a self-reference, and it was fixed.

However, when I try to delete a model instance which has a foreign key point to that self-reference model in admin, this error occurred again.

@coleifer
Owner

I was not able to replicate the maximum recursion error. Can you paste me some tracebacks as well as versions of peewe and flask-peewee you're using?

@wong2

........duplicated rows............

  File "/usr/local/lib/python2.6/dist-packages/peewee-0.9.2-py2.6.egg/peewee.py", line 1108, in follow_joins
    computed.extend(self.follow_joins(model, alias_map, alias_required, alias_count, seen))
  File "/usr/local/lib/python2.6/dist-packages/peewee-0.9.2-py2.6.egg/peewee.py", line 1108, in follow_joins
    computed.extend(self.follow_joins(model, alias_map, alias_required, alias_count, seen))
  File "/usr/local/lib/python2.6/dist-packages/peewee-0.9.2-py2.6.egg/peewee.py", line 1108, in follow_joins
    computed.extend(self.follow_joins(model, alias_map, alias_required, alias_count, seen))
  File "/usr/local/lib/python2.6/dist-packages/peewee-0.9.2-py2.6.egg/peewee.py", line 1083, in follow_joins
    field = from_model._meta.get_related_field_for_model(model, on)
  File "/usr/local/lib/python2.6/dist-packages/peewee-0.9.2-py2.6.egg/peewee.py", line 2298, in get_related_field_for_model
    if isinstance(field, ForeignKeyField) and field.to == model:
RuntimeError: maximum recursion depth exceeded while calling a Python object
[pid: 4527|app: 0|req: 214/394] <ip> () {50 vars in 1028 bytes} [Fri Apr  6 11:16:50 2012] POST /admin/user/delete/ => generated 0 bytes in 311 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
@coleifer
Owner

Is your user model self-referential? Here is what I have created to test:

class Categ(db.Model):
    parent = ForeignKeyField('self', null=True)
    name = CharField()

    def __unicode__(self):
        if self.parent:
            return '%s/%s' % (self.parent, self.name)
        return self.name

I am able to create instances several levels of nesting deep and do not get the error. Am i missing something? YOu might also try setting

delete_recursive=False
delete_collect_objects=False

Please feel free to join #peewee on freenode irc and we can discuss this there if you'dlike!

@coleifer
Owner

Quick update, it does appear that setting null=False will result in recursion error.

@coleifer
Owner

I've been doing some thinking about this one and I think that the logic is correct. The code in question is in peewee, collect_models around line 2533. Currently if a relation is nullable, collect_models will not recurse. Models organized into a self-referential foreign key should have a nullable foreign key, or you cannot possibly bootstrap the table without first creating a row that relates to itself.

If you want to see a potential solution, i've pushed one to peewee but will most likely not merge for above reasons. coleifer/peewee@ac900c2

@coleifer coleifer closed this Apr 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment