To clarify my question I would like to know if it is possible to idiomatically use the Django ORM whilst accessing CTE features. I imagine I could use CTE by writing raw SQL statements but the ability to use the ORM 'syntactic sugar' to bypass hand coding SQL statements was one of the original appeals of Django.
Does Django natively support common table expressions?
Asked Answered
Django doesn't support CTEs directly as these are not common to all databases (MySQL pre-8.0 didn't support it). There are packages that extend the capability of Django's ORM to support CTEs. One of these is django-cte. Note that it only supports PostgreSQL.
MySQL does now support CTEs, as does SQLite, Oracle, and PostgreSQL. All of which Django supports. –
Embassy
If someone is wondering: django-cte-forest is a fork of django-cte-trees. –
Codicodices
It seems that nowadays django-cte is a better option. It works with the existing support for CTEs in underlying database engines.
© 2022 - 2024 — McMap. All rights reserved.