加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 检查sqlalchemy中的表兼容性

发布时间:2020-12-20 13:40:56 所属栏目:Python 来源:网络整理
导读:我声明了一些表示远程数据库的表. 我想检查我的表定义是否与我连接的远程数据库匹配. 我有以下功能: def verify_db_tables(conn,metadata): """checks that the tables declared in metadata are actually in the db""" for table in metadata.tables.value
我声明了一些表示远程数据库的表.

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

我有以下功能:

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使用了一个类heiarchy,所以这将很好地工作

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读