Opened 7 years ago

Last modified 6 years ago

#28727 closed Bug

sqlite: CAST to DATE causes error — at Initial Version

Reported by: direx Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: SQLite Cast
Cc: Simon Charette 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

When trying to cast a date object to a DateField in annotate an exception is raised:

import datetime
from django.contrib.auth.models import User
from django.db import models
from django.db.models.functions import Cast

User.objects.all().annotate(today=Cast(datetime.date.today(), models.DateField()))

...

TypeError: expected string or buffer

This only seems to happen with SQLite, MySQL works as expected.

FYI, this is the SQL which is generated for SQLite:

SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined", CAST(2017-10-20 AS date) AS "today" FROM "auth_user"

Change History (0)

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