# event listener to apply StringDate on Table reflection
def listen_for_reflect (inspector, table, column_info):
"receive a column reflect event"
if isinstance(column_info['type'],DATETIME2):
column_info['type'] = StringDate
event.listen(Table,'column_reflect',listen_for_reflect)
engine = create_engine("some_db_url")
# create test tables (to mimic mssql temporal tables)
meta = MetaData()
Table('t', meta,
Column('Id', Integer, primary_key=True),
Column('SysVerStart', DATETIME2),
Column('SysVerEnd', DATETIME2)
)
Table('t_history', meta,
Column('Id', Integer),
Column('SysVerStart', DATETIME2),
Column('SysVerEnd', DATETIME2)
)
# create tables in our database
meta.create_all(engine)
# generate select statements using table reflection
meta.clear()
t = Table('t', meta, autoload=True, autoload_with=engine)
t_history = Table('t_history', meta, autoload=True, autoload_with=engine)
print('****************** StringDate TypeDecorator applied as expected to t :')
print(t.select())
print('****************** StringDate TypeDecorator applied as expected to t_history :')
print(t_history.select())
print('****************** StringDate TypeDecorator only applied to the first table in a union_all :')
print(t.select().union_all(t_history.select()))
print('****************** StringDate TypeDecorator only applied to the first table in a union :')
print(t_history.select().union(t.select()))