Opened 5 years ago

Closed 5 years ago

#31214 closed Bug (duplicate)

ORM query with Count(Subquery(...)) produces invalid SQL with Django 3.0.

Reported by: Alex Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords: 3.0, ORM, Postgres
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There is an ORM query in my app, which I wrote when Django 2.2 was used and it broke after migrating to 3.0:

    Targets.objects.filter(hgnc_sym=sym_list)
            .annotate(
                num_of_drugs=Count(
                    Subquery(Drug.objects.filter(id__in=OuterRef('drugs__id'),).distinct('id').values('id')),
                    distinct=True,
                )
            )

There are two models (Targets, Drug) with a M2M relation through targetdrug table. I am annotating each Targets record with count of related Drugs.
Django produces SQL queries that look almost the same except one brackets set.
Here is the SQL code produced by Django 2.2:

SELECT "targets"."hgnc_sym",
       COUNT(DISTINCT
               (SELECT DISTINCT ON (U0."id") U0."id"
                FROM "drug" U0
                WHERE U0."id" IN ("targetdrug"."drug_id"))) AS "num_of_drugs"
FROM "targets"
LEFT OUTER JOIN "targetdrug" ON ("targets"."id" = "targetdrug"."target_id")
WHERE "targets"."hgnc_sym" IN ('ABC')
GROUP BY "targets"."id";

and here is same SQL, produced by Django 3.0:

SELECT "targets"."hgnc_sym",
       COUNT(DISTINCT
               (SELECT DISTINCT ON (U0."id") U0."id"
                FROM "drug" U0
                WHERE U0."id" IN "targetdrug"."drug_id")) AS "num_of_drugs"
FROM "targets"
LEFT OUTER JOIN "targetdrug" ON ("targets"."id" = "targetdrug"."target_id")
WHERE "targets"."hgnc_sym" IN ('ABC')
GROUP BY "targets"."id";

The problem is that last SQL script misses brackets at line starting with WHERE U0."id" IN ....
Running that SQL results into the following error:

syntax error at or near ""targetdrug""
LINE 5:                 WHERE U0."id" IN "targetdrug"."drug_...

Postgres version:
PostgreSQL 11.4 (Debian 11.4-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
Django version:
python -c 'import django; print(django.VERSION)'
(3, 0, 2, 'final', 0)

Change History (2)

comment:1 by Alex, 5 years ago

Keywords: 3.0 ORM Postgres added

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #31135. Using __in lookup with OuterRef() is not supported, you should use Drug.objects.filter(id=OuterRef('drugs__id')) instead.

Note: See TracTickets for help on using tickets.
Back to Top