Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot delete broken tree. #69

Closed
zbyte64 opened this issue Jun 26, 2017 · 7 comments
Closed

Cannot delete broken tree. #69

zbyte64 opened this issue Jun 26, 2017 · 7 comments

Comments

@zbyte64
Copy link

zbyte64 commented Jun 26, 2017

Sometimes one cannot succeed in running fix_tree(destructive=True) and resorts instead to using objects.all().delete() to start fresh. But it seems that is not always possible if the tree is broken:

>>> Concept.objects.all().delete()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/treebeard/mp_tree.py", line 112, in delete
    parents[parentpath] = node.get_parent(True)
  File "/usr/local/lib/python3.6/site-packages/treebeard/mp_tree.py", line 1072, in get_parent
    self.__class__).objects.get(path=parentpath)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 385, in get
    self.model._meta.object_name
apps.focus.models.DoesNotExist: Concept matching query does not exist.

There should be a guaranteed programmatic way to reset the table state even if the tree paths are broken.

@zbyte64
Copy link
Author

zbyte64 commented Jun 26, 2017

Current hack around incase anyone else needs to do this:

f = Concept.objects.all()
from django.db.models import QuerySet
QuerySet.delete(f)

@jrief
Copy link
Member

jrief commented Jun 27, 2017

Please always add the Django version you are using.

@zbyte64
Copy link
Author

zbyte64 commented Jun 28, 2017

django==1.10.6
django-treebeard==4.1.2

@jhrr
Copy link

jhrr commented Jan 4, 2018

Had the same issue which I could fix with the above QuerySet hack but also managed to cause a KeyError when attempting fix_tree(destructive=True). Here's the traceback:

In [8]: ConditionGroup.fix_tree(destructive=True)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-8-67a4ab5ed8ee> in <module>()
----> 1 ConditionGroup.fix_tree(destructive=True)

~/.virtualenvs/fathom/lib/python3.6/site-packages/treebeard/mp_tree.py in fix_tree(cls, destructive)
    769
    770         if destructive:
--> 771             dump = cls.dump_bulk(None, True)
    772             cls.objects.all().delete()
    773             cls.load_bulk(dump, None, True)

~/.virtualenvs/fathom/lib/python3.6/site-packages/treebeard/mp_tree.py in dump_bulk(cls, parent, keep_ids)
    660             else:
    661                 parentpath = cls._get_basepath(path, depth - 1)
--> 662                 parentobj = lnk[parentpath]
    663                 if 'children' not in parentobj:
    664                     parentobj['children'] = []

KeyError: '0001'

Django==1.11.6
django-treebeard==4.2.0

@rollue
Copy link

rollue commented Jul 31, 2020

@jhrr hi there did you manage to fix the KeyError? I'm getting the same error when invoking dump_bulk method. The funny thing is Category.dump_bulk works the first time, but keyerror is raises after the first try. :(
I'm lost and need help with this 🙏

@jhrr
Copy link

jhrr commented Jul 31, 2020

@mhoonjeon sorry I didn't, I can't quite remember how I worked around it but I never got any further with the KeyError than this. Good luck!

@tabo
Copy link
Member

tabo commented Jan 18, 2021

There is a whole new implementation of fix_tree(destructive=True). Please file another ticket if there are other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants