-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
How to get ancestors per node for subquery? #54
Comments
Hey That's interesting. I didn't even know about Regarding your use case: I'm not sure what you're trying to do. Are you only trying to fetch the primary keys of the ancestors or are you also fetching the model instances of all ancestors? Or asked differently, does the Fetching the descendants of all nodes would be easier with |
The 'ArraySubquery was just to explain, that it needs to run inside a subquery, cause of the 'OuterRef'. The ancestors per node does return juat a common django 'QuerySet' whith that you can work in the common django way. In my use case i got a tree of ogc wms layers. And there is some inherite magic, where for example the maximum scale of a layer is inherited by the parent, if it is null on the current node. So i need to lookup the scale value from the ancestors. In my usecase i need a high performing query, which does no extra hits on the db. Take a look to my manager There is the queryset which returns the ancestors per node and many other querys which are based on that to return just the inherited values. |
So if you are inheriting values from ancestors it may make sense to write the SQL for the recursive CTE yourself. It's not that hard, especially if you do not have to support multiple databases and more so if you only have to support PostgreSQL. This is probably the "official" way to go since the Django ORM doesn't support CTEs at all yet. There's probably not a good alternative right now anyway if you are sure you need the performance and everything. |
In
django-mptt
i can subquery all ancestors per node by using the following code snippet:In
django-tree-queries
i thought i can do it like:But that results in an ValueError: This queryset contains a reference to an outer query and may only be used in a subquery.
Logically this can't work, cause the
ancestors
method of theTreeQuerySet
does the lookup if the passed of has notree_path
attribute:Do you have any ideas, how i can construct an object from the OuterRef to pass it in the
ancestors
method; Or any other way to get the ancestors per node for subquerys?The text was updated successfully, but these errors were encountered: