feincms / django-tree-queries

Adjacency-list trees for Django using recursive common table expressions. Supports PostgreSQL, sqlite, MySQL and MariaDB.
https://django-tree-queries.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
427 stars 27 forks source link

support querying sub-classes with multi-table inheritance #48

Closed olivierdalang closed 1 year ago

olivierdalang commented 1 year ago

Hey !

Currently, there seem to be a limitation with multi-table inheritance setup when querying subclasses, because the CTE assumes the parent_id and id columns are on the table of the model (while they are on the super model). I stumbled on this when trying to combine django-tree-queries and django-polymorphic (btw if you're aware of such a combination please let me know).

This fixes the issue by ensuring the CTE is built against the super model.

Two commits in the PR: first with failing tests, second wit actual fix.

matthiask commented 1 year ago

Thank you!

The documentation doesn't state it clearly but using TreeNode is supposed to be optional. I have added a new test for this and resolved the conflicts, let's see whether tests pass.

I'm wondering if there isn't a better way to resolve the model having the required parent foreign key; maybe you could try checking whether a parent FK exists? I'm not sure if that would be better though.

matthiask commented 1 year ago

Hi again

Thanks for your contribution! The test cases were excellent and the implementation helped a lot. I found a different way to fetch the base model and will push a new release soon.