Opened 7 years ago

Closed 7 years ago

#28317 closed Bug (invalid)

QuerySet.extra cannot reference a column name with a capital letter

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

Description

Minimal steps to recreate:

1) Create a model with a field that has a capital letter. eg:

class A(models.Model):
    baseValue = models.IntegerField(default=0)
    class Meta:
        app_label = 'myapp'

2) Refer to that column with QuerySet.extra, either as a select or as a where. eg:

A.objects.all().extra(select={'test' : 'baseValue'})

3) The result is that Django turns baseValue to basevalue and looks for a column with that name. However, the column name is baseValue:

Exception Value: column "basevalue" does not exist
LINE 1: SELECT (baseValue) AS "test", "myapp_a"."id", "myapp_a...
                ^
HINT:  Perhaps you meant to reference the column "myapp_a.baseValue".

Sadly, I need the extra feature for a 3-way join: t1.x=t2.x AND t1.y=t3.y AND t2.z=t3.z
I will create another ticket for that and reference this one.

Change History (1)

comment:1 by Simon Charette, 7 years ago

Resolution: invalid
Status: newclosed

Hello ekarat,

As you can see through the SQL generated by Django it's correctly generating SELECT (baseValue) AS "test" with the appropriate case but the issue here is that most database engines ignore identifier case unless quoted.

I'm not sure why you are not doing an annotate(test=F('baseValue')) which would take care of quoting it for you in this case but you should make sure to quote baseValue yourself if you want to use extra(select={'test': '"baseValue"'}).

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