gpt4 book ai didi

python - 检查 sqlalchemy 中的表兼容性

转载 作者:太空狗 更新时间:2023-10-30 01:57:46 26 4
gpt4 key购买 nike

我声明了一些表示远程数据库的表。

我想检查我的表定义是否与我连接的远程数据库匹配。

我有以下功能:

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 使用类层次结构,所以这会很好地工作

    if not instance(check_column.type, column.type.__class__):

关于python - 检查 sqlalchemy 中的表兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22296595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com