Giter Site home page Giter Site logo

Improve documentation about django-cte HOT 3 CLOSED

dimagi avatar dimagi commented on July 22, 2024 1
Improve documentation

from django-cte.

Comments (3)

millerdev avatar millerdev commented on July 22, 2024
  • With(...) constructs a common table expression to be placed in a SQL WITH clause.
  • .join(...) adds a JOIN expression to the SQL FROM clause, joining the CTE query to some other queryset or model.
  • .with_cte(...) adds a CTE constructed with With(...) to a WITH clause on the queryset.

Anatomy of the resulting SQL, slightly simplified for purposes of explanation.

WITH RECURSIVE "cte" AS (  -- WITH clause added by .with_cte(...)

    -- CTE query constructed in With(...)
    SELECT
        "tests_order"."region_id",
        SUM("tests_order"."amount") AS "total"
    FROM "tests_order"
        INNER JOIN "tests_region" ON (
            "tests_order"."region_id" = "tests_region"."name"
        )
    WHERE "tests_region"."parent_id" = 'sun'
    GROUP BY "tests_order"."region_id"
)
SELECT
    "tests_order"."id",
    "tests_order"."region_id",
    "tests_order"."amount",
    "cte"."total" AS "region_total"
FROM "tests_order"
INNER JOIN "cte" ON "tests_order"."region_id" = ("cte"."region_id")  -- JOIN clause added by .join(...)
ORDER BY "tests_order"."amount" ASC

The above query (except for comments and basic formatting) was produced by adding print(orders.query) to TestCTE.test_simple_cte_query and then running nosetests tests/test_cte.py:TestCTE.test_simple_cte_query --nocapture

Does that make things more clear?

from django-cte.

bernd-wechner avatar bernd-wechner commented on July 22, 2024

Thanks, I worked that all out since and field a PR to simplify it a little by removing the need for .with_cte().. And I'll PR a README improvement some time too if you're content with the default CTE PR.

Another minor hassle I found the hard way along the way using this (otherwise brilliant addition to Django landscape - so brilliant it should be absorbed into Django as it solves a huge problem with Django namely querying on an encapsulated queryset) is that the result of the .join() includes only the fields from its argument and not the fields from the CTE, which need explicitly to be added again with an annotation as and when needed. That took me a long time to work out, and SQL dumps.

I might try and tidy that up a bit too. We shall see.

from django-cte.

millerdev avatar millerdev commented on July 22, 2024

New documentation was added in #54. Please file new issues if there are gaps.

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.