django-treebeard icon indicating copy to clipboard operation
django-treebeard copied to clipboard

Multi-table inheritance: `add_root` works the first time but not the second time

Open gnpivo opened this issue 1 year ago • 0 comments

Hi. To summarize this issue, calling add_root on a model that inherits from a Django Treebeard node model via multi-table inheritance will create an entity for both the base model and the inheriting model, but it only does this the first time; calling add_root again will only create an instance of the base model, not the inheriting model. To illustrate here are some example models:

from django.db import models
from treebeard.mp_tree import MP_Node


class BaseNode(MP_Node):
    name = models.CharField(max_length=100)
    node_order_by = ["name"]

    def __str__(self):
        return self.name


class DerivedNode(BaseNode):
    pass

Here is a detailed set of steps to reproduce the issue:

  1. Start a new Django project and app.
  2. Replace the contents of the models.py file with the snippet given above.
  3. Make and perform a migration.
  4. Open the shell with python manage.py shell.
  5. Import the derived model: from app.models import DerivedNode.
  6. Add a root: DerivedNode.add_root(name="Foo").
  7. Run DerivedNode.objects.all() and observe that <DerivedNode: Foo> was created successfully.
  8. Add another root: DerivedNode.add_root(name="Bar").
  9. Run DerivedNode.objects.all() and observe that there is still just <DerivedNode: Foo>; there is no <DerivedNode: Bar>.

I expect to see two DerivedNode objects because I called the add_root function on it twice, but I only see one DerivedNode object. It would seem that because sorted insertion is being delegated to the add_sibling method on the node obtained via get_last_root_node (like on line 319 of mp_tree.py), the node is being created as an instance of the base model and not the derived one. One simple fix would be to just not do that, instead creating the node from scratch based on the path of the last root node, but I wanted to confirm that my expectations of the library's behavior were correct before submitting a pull request.

gnpivo avatar May 31 '24 00:05 gnpivo