Re: [Django] #34346: QuerySet ordered by annotation with name used by select_related() field crashes with AmbiguousColumn.

47 views
Skip to first unread message

Django

unread,
Feb 17, 2023, 2:11:26 PM2/17/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Mohamed
| Nabil Rady
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mohamed Nabil Rady):

* owner: nobody => Mohamed Nabil Rady
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 17, 2023, 2:54:09 PM2/17/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Mohamed
| Nabil Rady
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Mohamed, thanks. Do you have time to fix it over the weekend? This is a
release blocker for Django 4.2, so we should fix it by Monday.

--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:3>

Django

unread,
Feb 17, 2023, 3:48:39 PM2/17/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Mohamed
| Nabil Rady
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

If not I have a pretty good idea on how to fix it by excluding `Col`
instances from order by selected replacements.

--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:4>

Django

unread,
Feb 17, 2023, 6:10:42 PM2/17/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Mohamed
| Nabil Rady
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mohamed Nabil Rady):

I am not that familiar with the codebase so I can't make any promises, so
I guess it's for the best to leave another person with the bug.

I found out the issue happens because before the regression in
`django.db.models.sql.compiler.SQLCompiler.get_order_by` would return a
`Col` object, but it now it returns `Ref` object which points to the
correct column but its `as_sql` method returns the annotated value name
instead of the unambiguous SQL.

I am not sure to what extent is this information useful or if it's
obvious, but as I said I am not that familiar with the codebase, and I am
not sure what `Ref` objects are responsible for.

--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:5>

Django

unread,
Feb 17, 2023, 8:08:45 PM2/17/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: (none)
Type: Bug | Status: new

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mohamed Nabil Rady):

* owner: Mohamed Nabil Rady => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:6>

Django

unread,
Feb 17, 2023, 8:22:24 PM2/17/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Simon
| Charette
Type: Bug | Status: assigned

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* owner: (none) => Simon Charette


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:7>

Django

unread,
Feb 19, 2023, 11:54:18 PM2/19/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/16571 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:8>

Django

unread,
Feb 20, 2023, 12:21:53 AM2/20/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Simon
| Charette
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"278881e37619278789942513916acafaa88d26f3" 278881e3]:
{{{
#!CommitTicketReference repository=""
revision="278881e37619278789942513916acafaa88d26f3"
Fixed #34346 -- Ordered selected expressions by position.

Used the same approach as for #34176 by using selected expressions
position to prevent ambiguous aliases in collisions.

Thanks henribru for the report.

Regression in 04518e310d4552ff7595a34f5a7f93487d78a406.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:9>

Django

unread,
Feb 20, 2023, 12:23:12 AM2/20/23
to django-...@googlegroups.com
#34346: QuerySet ordered by annotation with name used by select_related() field
crashes with AmbiguousColumn.
-------------------------------------+-------------------------------------
Reporter: henribru | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"aab25a69dd09e6717ff86175ff62c29b847a7791" aab25a6]:
{{{
#!CommitTicketReference repository=""
revision="aab25a69dd09e6717ff86175ff62c29b847a7791"
[4.2.x] Fixed #34346 -- Ordered selected expressions by position.

Used the same approach as for #34176 by using selected expressions
position to prevent ambiguous aliases in collisions.

Thanks henribru for the report.

Regression in 04518e310d4552ff7595a34f5a7f93487d78a406.

Backport of 278881e37619278789942513916acafaa88d26f3 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34346#comment:10>

Reply all
Reply to author
Forward
0 new messages