-
Notifications
You must be signed in to change notification settings - Fork 47
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
CTE statement is not being generated #8
Comments
This is caused by a "feature" of django that removes a UNION clause if django can deduce that the unioned query will return no rows (when the WHERE clause will always evaluate to false, for example). Prior to django-cte, this was probably a safe thing to do since standard union queries do not reference names defined outside of the query as recursive CTEs do. My guess is the filter on the first CTE query has an empty list: return Annotation.objects.filter(
# start with root nodes
id__in=annotation_ids # <---- annotation_ids is probably an empty list You can work around this by checking if the query is empty first, and not doing the CTE in that case: roots = Annotation.objects.filter(id__in=annotation_ids)
if roots.query.is_empty():
# construct a query that does not use a CTE since the CTE is empty
...
else:
# construct CTE query as in your example
... Another way to work around it would be to add an expression that will fool django. Something like this (although I'm fairly certain return Annotation.objects.filter(
# start with root nodes
Q(id__in=annotation_ids) |
Q(Literal('1 = 2')) # <--- broken example. Literal doesn't work like that
).values(
... |
Thanks! That clarification really made it a lot clearer! I'm wondering if you think it's worth the trouble of trying to detect (and warn users) somehow? |
Adding a warning is good idea. I'll see what I can do. |
Cool! I would have given it a shot myself, but to be honest I don't quite understand how this works behind the scenes, definitely not well enough to do something like that. |
If the original query has no root * Prevent crash due to invalid query optimisation of the CTE. see dimagi/django-cte#8 * The returned query needs to support the expected annotations or code that consumes this will crash
First of all, sorry for posting this as an issue. I could not think of any other place to ask. This is the first time I'm trying django-cte so my mistake could be silly, however I mostly copied the README example.
This is my CTE code:
This is the resulting SQL query:
This is the error I'm getting:
Thanks for reading through!
The text was updated successfully, but these errors were encountered: