Giter Site home page Giter Site logo

Comments (4)

millerdev avatar millerdev commented on July 3, 2024

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 Literal doesn't work this way so this is not a working example):

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(
    ...

from django-cte.

nirizr avatar nirizr commented on July 3, 2024

Thanks! That clarification really made it a lot clearer!
I've managed to notice I had a typo in the parameter I was reading from the query.

I'm wondering if you think it's worth the trouble of trying to detect (and warn users) somehow?

from django-cte.

millerdev avatar millerdev commented on July 3, 2024

Adding a warning is good idea. I'll see what I can do.

from django-cte.

nirizr avatar nirizr commented on July 3, 2024

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.

from django-cte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.