You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import datetime
from pypika import Query
from pypika import Table
d = datetime.date(2000, 1, 1)
dt = datetime.datetime(2022, 1, 1, 1, 1, 1)
ti = datetime.time(1, 1, 1)
t = Table("t")
q1 = Query.into(t).insert(d)
q2 = Query.into(t).insert(dt)
q3 = Query.into(t).insert(ti)
>>> q1
INSERT INTO "t" VALUES ('2000-01-01')
>>> q2
INSERT INTO "t" VALUES ('2022-01-01T01:01:01')
>>> q3
INSERT INTO "t" VALUES (01:01:01)
As you can see, the conversion for datetime.date and datetime.datetime is correct - values are converted using datetime.date.isoformat and datetime.datetime.isoformat and enclosed with single quotes. Unfortunately, values for datetime.time are not enclosed with quotes which leads to invalid queries. This is due to the fact that in ValueWrapper values datetime.date and datetime.datetime fall under isinstance(value, date) which does the proper conversion but datetime.time falls under str(value).
My proposal is to enhance the condition to also cover datetime.time:
pypika/terms.py:443
if isinstance(value, date) or isinstance(value. time):
return cls.get_formatted_value(value.isoformat(), **kwargs)
The text was updated successfully, but these errors were encountered:
hfaua
pushed a commit
to hfaua/pypika
that referenced
this issue
Apr 4, 2025
Consider following code:
As you can see, the conversion for
datetime.date
anddatetime.datetime
is correct - values are converted usingdatetime.date.isoformat
anddatetime.datetime.isoformat
and enclosed with single quotes. Unfortunately, values fordatetime.time
are not enclosed with quotes which leads to invalid queries. This is due to the fact that inValueWrapper
valuesdatetime.date
anddatetime.datetime
fall underisinstance(value, date)
which does the proper conversion butdatetime.time
falls understr(value)
.My proposal is to enhance the condition to also cover
datetime.time
:The text was updated successfully, but these errors were encountered: