Python 检查sqlalchemy中的表兼容性
我声明了一些表示远程数据库的表 我想检查我的表定义是否与连接到的远程数据库匹配 我有以下功能:Python 检查sqlalchemy中的表兼容性,python,sql,database,sqlalchemy,Python,Sql,Database,Sqlalchemy,我声明了一些表示远程数据库的表 我想检查我的表定义是否与连接到的远程数据库匹配 我有以下功能: def verify_db_tables(conn, metadata): """checks that the tables declared in metadata are actually in the db""" for table in metadata.tables.values(): check = sqlalchemy.MetaData()
def verify_db_tables(conn, metadata):
"""checks that the tables declared in metadata are actually in the db"""
for table in metadata.tables.values():
check = sqlalchemy.MetaData()
check.reflect(conn, table.schema, True, (table.name,))
check = check.tables[table.key]
for column in table.c:
if column.name not in check.c:
raise Exception("table %s does not contain column %s" %
(table.key, column.name))
check_column = check.c[column.name]
if check_column.type != column.type:
raise Exception("column %s.%s is %s but expected %s" %
(table.key, column.name, check_column.type, column.type))
我特别不关心表中是否有额外的列,也不关心是否有额外的表
但是,当我运行此代码时,会出现以下错误:
Exception: column dx.mail_data.message_id is INTEGER but expected INTEGER
如何检查反射表中的列是否与我的定义的类型相同?SQLite使用类HEIARCY,因此这将很好地工作
if not instance(check_column.type, column.type.__class__):
啊!谢谢事后看来很明显。