Does Django natively support common table expressions?
Asked Answered
G

2

16

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.

Goulder answered 6/7, 2013 at 1:40 Comment(0)
N
12

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.

Night answered 12/9, 2013 at 7:43 Comment(2)
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
E
2

It seems that nowadays django-cte is a better option. It works with the existing support for CTEs in underlying database engines.

Ehlke answered 13/9, 2022 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.