Opened 5 years ago

Closed 5 years ago

#14569 closed (wontfix)

Date field expression raw query returns unicode instead of datetime object in sqlite

Reported by: robin Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords: raw query date sqlite postgresql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Using sqlite

>>> m = MyModel.objects.raw('select id,max(date_added) as d from app_modelname group by id')
>>> m[0].d
u'2010-10-26 04:11:33.123513'

Whereas with postgresql

>>> m[0].d
datetime.datetime(2010, 10, 26, 4, 11, 33)

I believe the result returning a datetime object by postgresql is correct, whereas sqlite is not.

Change History (2)

comment:1 Changed 5 years ago by dmoisset

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

The problem here is that you're using an expression, not a column name. The raw queryset maps all columns to the correct fields, but if you use arbitrary expressions it lets the db value through... and sqlite has no datetime values, just numbers and unicode strings.

comment:2 Changed 5 years ago by ramiro

  • Resolution set to wontfix
  • Status changed from new to closed
  • Summary changed from Raw query for date field returns unicode instead of datetime object in sqlite to Date field expression raw query returns unicode instead of datetime object in sqlite

So, unfortunately we can't fix this in Django because that means it would need to interpret all raw SQL queries and deduce the correct data type of the query result. Closing as wontfix

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