sqlalchemy can't read null dates from sqlite3 (0000-00-00): ValueError: year is out of range
Question:
When I try to query a database containing dates such as 0000-00-00 00:00:00
with sqlachemy
, I get ValueError: year is out of range
.
Here’s the db dump:
Here’s the stacktrace:
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/engine/result.py" in items
163. return [(key, self[key]) for key in self.keys()]
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/engine/result.py" in <listcomp>
163. return [(key, self[key]) for key in self.keys()]
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/engine/result.py" in __getitem__
90. return processor(self._row[index])
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/processors.py" in process
48. return type_(*list(map(int, m.groups(0))))
Exception Type: ValueError at /
Exception Value: year is out of range
Is this normal ? Can sqlalchemy read dates like that ? Is this a python limitation ? Is there a workaround to keep the date as-is (not converting to None) ?
Answers:
Got the answer via inklesspen
on IRC: Python datetime representation has minimum year and it’s 1
I was able to bypass the problem by using sqlalchemy.text
from sqlalchemy import text
with engine.connect() as conn:
result = conn.execute(text("select * from table"))
....
When I try to query a database containing dates such as 0000-00-00 00:00:00
with sqlachemy
, I get ValueError: year is out of range
.
Here’s the db dump:
Here’s the stacktrace:
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/engine/result.py" in items
163. return [(key, self[key]) for key in self.keys()]
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/engine/result.py" in <listcomp>
163. return [(key, self[key]) for key in self.keys()]
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/engine/result.py" in __getitem__
90. return processor(self._row[index])
File "/home/rob/.virtualenvs/calif/lib/python3.5/site-packages/sqlalchemy/processors.py" in process
48. return type_(*list(map(int, m.groups(0))))
Exception Type: ValueError at /
Exception Value: year is out of range
Is this normal ? Can sqlalchemy read dates like that ? Is this a python limitation ? Is there a workaround to keep the date as-is (not converting to None) ?
Got the answer via inklesspen
on IRC: Python datetime representation has minimum year and it’s 1
I was able to bypass the problem by using sqlalchemy.text
from sqlalchemy import text
with engine.connect() as conn:
result = conn.execute(text("select * from table"))
....