python - sqlalchemy: Making schema reflection find/use a custom type for all instances -


i'm trying override sqlalchemy datetime2 type (from ms sql server) discard digits of subsecond precision if necessary coerce native python datetime type, described in replacing bind result processing of existing types section of docs, so:

import re import sqlalchemy.dialects 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)                 x in self._reg.match(value).groups()])         else:             return value      def adapt(self, impltype):         return truncatingprecisiondatetime2() 

however, when call meta.reflect(), , inspect type of 1 column of type datetime2 in database, it's instance of sqlalchemy.dialects.mssql.base.datetime2 rather truncatingprecisiondatetime2. need change globally datetime2 instances?

this unofficial hell, , i'm not sure work on mssql opposed postgresql (as changelog might seem suggest), replacing 'datetime2' item on sqlalchemy.dialects.mssql.base.ischema_names dictionary custom subclass, may able override sqlalchemy type used reflections of type, if official, recommended way of modifying result processing fails.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -