I am trying to override the SQLAlchemy type DATETIME2
(from MS SQL Server) in order to discard extra digits of subgrid precision, if necessary, to force Python to have its own type of date and time, as described in the section Replacing processing of the result of binding existing document section types , for example:
import re
import sqlalchemy.dialects as sa_dialects
class TruncatingPrecisionDATETIME2(sa_dialects.mssql.base.DATETIME2):
__visit_name__ = 'DATETIME2'
_reg = re.compile(r"(\d+):(\d+):(\d+)(?:\.(\d{0,6})\d*)?")
def process_result_value(self, value, dialect):
if isinstance(value, util.string_types):
return datetime.time(*[
int(x or 0)
for x in self._reg.match(value).groups()])
else:
return value
def adapt(self, impltype):
return TruncatingPrecisionDATETIME2()
However, when I call meta.reflect()
and check the type of the type column DATETIME2
in the database, it is an instance sqlalchemy.dialects.mssql.base.DATETIME2
, not TruncatingPrecisionDATETIME2
. What do I need to do to change this globally for all instances of DATETIME2?
source
share