Opened 3 years ago

Closed 3 years ago

#29442 closed Bug (duplicate)

wrong sql for cast datetime to date in sqlite backend

Reported by: Mauro Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: sqlite3 cast
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


"data_dimissione" is datetime field and I need list of days, so my queryset is like

qs = objects.all().annotate(date_only=Cast("data_dimissione", DateField())).values_list('date_only', flat=True).distinct()

It works on mysql , but django 1.11.10-1~bpo9+1 with sqlite3 3.16.2-5+deb9u1 raise TypeError

  File  "../site-packages/django/db/backends/sqlite3/", line 232, in convert_datefield_value
    raise e
TypeError: expected string or buffer

Reason is sql query is like

SELECT DISTINCT CAST("ps_ps"."data_dimissione" AS date) AS "date_only", ........

but data results is

Correct sql seems
SELECT DISTINCT date("ps_ps"."data_dimissione" ) AS "date_only", .

Change History (1)

comment:1 Changed 3 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

Duplicate of #28727 (fixed in Django 2.1).

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