Datetime '0000-00-00' maps to None
rodrigovalin opened this issue · comments
The issue here is that both NULL datetimes and '0000-00-00' datetimes in the context of MySQL have the same meaning in the context of MySQLdb (both are represented as None) thus data will be lost in case of (as an example) data being copied from one data base to another.
I'm not seeing a solution here - yet -, as you can't have a Datetime object with day, month and year as 0 values.
My solution, for my specific problem is this (using MySQLdb-1.2.4, installed with pip), on times.py (line 79):
def Date_or_None(s):
if s[0:4] == '0000': # this is my solution
return s
try: return date(*[ int(x) for x in s.split('-',2)])
except: return None
So I'm returning the original Datetime representation string (lines 2 and 3 of code block were added by me).
It's by design (obviously), but I am considering returning MySQL date values which cannot be turned into Python dates as the original string value, for version 1.3.
I guess introducing backward-incompatible changes in minor version is generally a bad idea. Not only it may break a lot of code out there (which will be hard-to-debug as no string was expected). But also it'll make date values more inconsistent. Now it's clear that either you get datetime.datetime
or NoneType
for a date column value. So I think it's a special enough case for a developer who absolutely needs this quirky zero values, to either cast columns in the query to CHAR
directly, or to redefine MySQLdb converters.