Opened 2 years ago

Closed 2 years ago

#34079 closed Bug (invalid)

Excessive parentheses when generating a query

Reported by: Lelikov Owned by: Aman Pandey
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords:
Cc: Peter Lithammer, Simon Charette Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ORM:

Model.objects.filter(field=Func(F("user__notification_email_types"), function="ANY")

SQL:

..."app_model"."filed" = (ANY("app_user"."notification_email_types")))

Error:

syntax error at or near "ANY"

The reason for the code change in version 4.0. The excessive parentheses around the function ANY
django/db/models/lookups.py

if sql and sql[0] != "(":
    sql = "(%s)" % sql

In version 3.2 everything worked correctly

Change History (5)

comment:1 by Lelikov, 2 years ago

Type: UncategorizedBug

comment:2 by Giebisch, 2 years ago

Im pretty sure I had a similar error a few weeks ago. I can't test right now, but if the error still persists, I will be happy to look into it :)

comment:3 by Aman Pandey, 2 years ago

i would like to look into it

comment:4 by Aman Pandey, 2 years ago

Owner: changed from nobody to Aman Pandey
Status: newassigned

comment:5 by Mariusz Felisiak, 2 years ago

Cc: Peter Lithammer Simon Charette added
Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: assignedclosed

Thanks for the report, however this behavior was intentionally changed in 170b006ce82b0ecf26dc088f832538b747ca0115 (see also #32673) and we cannot revert it without reintroducing regressions.

As far as I'm aware,ANY requires ARRAY or a subquery on the right-hand side, so I'm not sure how this could work for you 🤔 Nevertheless you can always creating a custom lookup, the following example works for me:

class Any(Lookup):
    def as_sql(self, compiler, connection):
        lhs, lhs_params = self.process_lhs(compiler, connection)
        rhs, rhs_params = self.process_rhs(compiler, connection)
        params = (*lhs_params, *rhs_params)
        return "%s = ANY(%s)" % (lhs, rhs), params

> Note.objects.filter(Any(F("note"), Subquery(Annotation.objects.values("name"))))
Note: See TracTickets for help on using tickets.
Back to Top