Skip to content

Support to perform query that includes select_for_update() and select_related()#137

Open
jamesbeith wants to merge 1 commit intodimagi:mainfrom
jamesbeith:jamesbeith/select-for-update-related
Open

Support to perform query that includes select_for_update() and select_related()#137
jamesbeith wants to merge 1 commit intodimagi:mainfrom
jamesbeith:jamesbeith/select-for-update-related

Conversation

@jamesbeith
Copy link
Copy Markdown

@jamesbeith jamesbeith commented Feb 27, 2026

After reading this Row Locks With Joins Can Produce Surprising Results in PostgreSQL blog post I was curious as to whether I could reproduce this query using django-cte.

WITH c AS (
    SELECT * FROM car WHERE id = 1
    FOR NO KEY UPDATE
)
SELECT * FROM c
JOIN owner ON c.owner_id = owner.id;

Please see the test in this PR, however unfortunately it results in the following error.

TypeError: Cannot call select_related() after .values() or .values_list()

I can make the test pass by removing this qs._fields = () line from here but that unfortunately causes the test_django52_annotate_model_field_name_after_queryset test to fail with.

ValueError: The annotation 'id' conflicts with a field on the model.

Is there another way to get the same query I'm after using django-cte?

The intention is to lock the order, and only the order, whilst joining to the user table so we only perform a single query.
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