Opened 6 years ago
Closed 6 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 |
Description
"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/operations.py", 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
2018|......
see https://code.djangoproject.com/ticket/28727#comment:6
Correct sql seems
SELECT DISTINCT date("ps_ps"."data_dimissione" ) AS "date_only", .
Duplicate of #28727 (fixed in Django 2.1).