Opened 4 years ago

Closed 4 years ago

#24986 closed Bug (fixed)

Selecting distinct on lower() function throws AttributeError("'Func' object has no attribute 'column'")

Reported by: wbar Owned by: Valentina Mukhamedzhanova
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: F(), ORM, distinct
Cc: josh.smeaton@… 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 (last modified by wbar)

I have simple table:

class Author(models.Model):
    name = models.CharField(max_length=40)

I want to write Query using Django ORM to be similar to:

SELECT DISTINCT LOWER(name) from my_app_author;

I finally ended with:

Author.objects.annotate(
    name_lower=Func(F('name'), function='lower')
).distinct('name_lower')

But I'm receiving error:

Traceback (most recent call last):
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 138, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 162, in __iter__
    self._fetch_all()
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 238, in iterator
    results = compiler.execute_sql()
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql
    sql, params = self.as_sql()
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 381, in as_sql
    distinct_fields = self.get_distinct()
  File "/opt/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 545, in get_distinct
    result.append("%s.%s" % (qn(alias), qn2(target.column)))
AttributeError: 'Func' object has no attribute 'column'

but when i write:

Author.objects.annotate(
    name_lower=Func(F('name'), function='lower')
).distinct('something')

I'm receiving an error:

django.core.exceptions.FieldError: 
Cannot resolve keyword 'something' into field. 
Choices are: id, name, name_lower, src_id

As database driver I'm using 'ENGINE': 'django.contrib.gis.db.backends.postgis'

Change History (14)

comment:1 Changed 4 years ago by wbar

comment:2 Changed 4 years ago by wbar

Description: modified (diff)

comment:3 Changed 4 years ago by Josh Smeaton

Severity: Release blockerNormal
Triage Stage: UnreviewedAccepted
Version: 1.8master
Author.objects.annotate(
    name_lower=Func(F('name'), function='lower')
).distinct('name_lower')

I don't think we implemented Expression support for distinct(). Accepting on that basis - it shouldn't (famous last words..) be too difficult to implement.

but when i write:

Author.objects.annotate(
    name_lower=Func(F('name'), function='lower')
).distinct('something')

I've removed release blocker and 1.8 target, as this is a new feature not previously available. It should be able to go into 1.9 provided there's a patch, but I'm not sure that we'd backport a fix to 1.8.

I don't know why you'd think that'd work, especially since the error tells you that "something" isn't actually a field on the model. Am I missing something here?

comment:4 Changed 4 years ago by Anssi Kääriäinen

+1 to non-release-blocker. It would be nice to have this, but I don't think this is a severe limitation in the expressions feature. In other words, this is just an ordinary bug.

comment:5 Changed 4 years ago by wbar

I don't know why you'd think that'd work, especially since the error tells you that "something" isn't actually a field on the model. Am I missing something here?

So why You can see name_lower on the choices list ?

Choices are: id, name, name_lower, src_id

comment:6 Changed 4 years ago by Josh Smeaton

Because name_lower is added to the field list when you use the annotation. The field "something" does not exist and was not annotated in your example.

comment:7 Changed 4 years ago by Shai Berger

I believe the OP was only using something to point out that name_lower doesn't work in spite of being given as an option in the error message.

On a separate note: You should be able to get the query you described initially with

Author.objects.annotate(
    name_lower=Func(F('name'), function='lower')
).values('name_lower').distinct()

or something similar.

comment:8 Changed 4 years ago by wbar

Thx,
@shaib this worked for me :)

Last edited 4 years ago by wbar (previous) (diff)

comment:9 Changed 4 years ago by Valentina Mukhamedzhanova

Owner: changed from nobody to Valentina Mukhamedzhanova
Status: newassigned

comment:10 Changed 4 years ago by Josh Smeaton

Cc: josh.smeaton@… added

comment:11 Changed 4 years ago by Tim Graham

Has patch: set

comment:12 Changed 4 years ago by Tim Graham

I think the patch looks okay (I proposed some cosmetic improvements to the test). Josh, could you check it?

comment:13 Changed 4 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:14 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 1f7b25c:

Fixed #24986 -- Added support for annotations in DISTINCT queries.

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