Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#36288 closed Bug (fixed)

Regression in values_list() with duplicated field names

Reported by: Claude Paroz Owned by: Simon Charette
Component: Database layer (models, ORM) Version: 5.2
Severity: Release blocker Keywords:
Cc: Simon Charette Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Django 5.2, a call to values_list("field", "field") will return tuples with a single member, as duplicated field names seem to be filtered out somewhere.

I suspect a regression caused by #28900.

Change History (7)

comment:1 by Natalia Bidart, 5 months ago

Cc: Simon Charette added
Triage Stage: UnreviewedAccepted

Thank you Claude for the report! I have reproduced with the following:

>>> import django
>>> django.VERSION
(6, 0, 0, 'alpha', 0)
>>> [i for i in User.objects.all().values_list("username", "username")]
[('admin',)]

Versus:

>>> import django
>>> django.VERSION
(5, 1, 7, 'final', 0)
>>> from django.contrib.auth.models import User
>>> [i for i in User.objects.all().values_list("username", "username")]
[('admin', 'admin')]

Now, I'm not sure this is a bug? I mean there is certainly a change in behavior, but to me the 5.1 result feels "buggy", so in a way this "was fixed" in 5.2.
On the other hand, the DB query does duplicate the columns so perhaps this *is* a regression:

psql (16.8 (Ubuntu 16.8-0ubuntu0.24.04.1))
Type "help" for help.

djangotest=# select username, username from auth_user;
 username | username 
----------+----------
 admin    | admin
(1 row)

Claude, could you indulge me and share in the ticket the use case for getting the repeated fields in the values_list result? Accepting in the meantime.

comment:2 by Simon Charette, 5 months ago

Owner: set to Simon Charette
Status: newassigned

This is effectively a regression caused by #28900 (65ad4ade74dc9208b9d686a451cd6045df0c9c3a) as sql.Query.selected is now a dict[alias, Expression] and since values_list defaults to using the provided strings as field names they get considered to be a single one.

We'll most likely have to generate an alias for the existing members with colliding names.

Last edited 5 months ago by Simon Charette (previous) (diff)

comment:3 by Simon Charette, 5 months ago

Has patch: set

comment:4 by Claude Paroz, 5 months ago

Thanks for the quick reaction! FWIW, the use case that revealed this was a QuerySet to produce tuples for form choices where the value and the verbose parts were identical.

comment:5 by Sarah Boyce, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:6 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In 21f8be7:

Fixed #36288 -- Addressed improper handling of duplicates in values_list().

Now that selected aliases are stored in sql.Query.selected: dict[str, Any]
the values_list() method must ensures that duplicate field name references are
assigned unique aliases.

Refs #28900.

Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a.

Thanks Claude for the report.

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

In b97af5e:

[5.2.x] Fixed #36288 -- Addressed improper handling of duplicates in values_list().

Now that selected aliases are stored in sql.Query.selected: dict[str, Any]
the values_list() method must ensures that duplicate field name references are
assigned unique aliases.

Refs #28900.

Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a.

Thanks Claude for the report.

Backport of 21f8be76d43aa1ee5ae41c1e0a428cfea1f231c1 from main.

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