Skip to content

Support Django 5.2 #108

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

simwr872
Copy link

@simwr872 simwr872 commented Apr 5, 2025

Hello,
A suggestion for how to support Django 5.2.

  • set_values() before add_annotation() since annotations now populate selected field.
  • Reference implicitly annotated field before trying to resolve reference the normal way.
  • Add annotations to cte table for selected fields not already annotated.

I modified 2 tests to explicitly select foreign key ID. In Django 5.2 foreign key ID's would be annotated to the foreign key name. Let me know if it is preferred to join on the foreign key name instead.

test_heterogeneous_filter_in_cte now works.

For some context, I asked a question in the Django forums: https://forum.djangoproject.com/t/selecting-primary-key-in-5-2/40215.

@simwr872
Copy link
Author

simwr872 commented Apr 10, 2025

I think I solved this with 9257e6b but I will leave the initial comment below.


Upon further tinkering, this does not fully solve 5.2 compatibility. Given the example:

cte = With.recursive(lambda cte: Region.objects.values("pk").union(cte.join(Region, parent=cte.col.pk).values("pk")))
queryset = cte.queryset().with_cte(cte)
print(queryset.query)

Difference between SQL generated pre/post 5.2:

@@ -1,8 +1,8 @@
 WITH RECURSIVE "cte" AS
-  (SELECT "region"."name"
+  (SELECT "region"."name" AS "pk"
    FROM "region"
-   UNION SELECT "region"."name"
+   UNION SELECT "region"."name" AS "pk"
    FROM "region"
    INNER JOIN "cte" ON "region"."parent_id" = ("cte"."name"))
-SELECT "cte"."name"
+SELECT "cte"."name" AS "pk"
 FROM "cte"

Which is invalid because the last select SELECT "cte"."name" column no longer exists (this PR would only swap out cte.name to cte.pk on the JOIN-clause). I need some help on how to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant