如何从供应商特定的数据类型中查找 SQLAlchemy 通用数据类型 [英] How to find SQLAlchemy generic datatype from vendor-specific datatype
问题描述
给定供应商特定的 SQLAlchemy 数据类型,例如 sqlalchemy.dialects.postgresql.INTEGER
,找到与该供应商特定类型最对应的通用 SQLAlchemy 数据类型的最佳方法是什么?
Given a vendor-specific SQLAlchemy datatype, such as sqlalchemy.dialects.postgresql.INTEGER
, what is the best way to find the generic SQLAlchemy datatype that best corresponds to this vendor-specific type?
例如,如果我有特定于供应商的类型 sqlalchemy.dialects.postgresql.INTEGER
,我想映射回 sqlalchemy.sql.sqltypes.Integer
.
For instance, if I have the vendor-specific type sqlalchemy.dialects.postgresql.INTEGER
, I would want to map back to sqlalchemy.sql.sqltypes.Integer
.
我确实理解在某些情况下这是不可能的,例如,如果数据库支持没有直接映射回 SA 通用数据类型的深奥类型.我主要只对直接映射回 SA 通用数据类型的数据类型感兴趣.
I do understand that there might be situations where this isn't possible, for instance if the database supports an esoteric type that doesn't have a straightforward mapping back to an SA generic datatype. I'm mainly just interested in the datatypes that do have a straightforward mapping back to an SA generic datatype.
推荐答案
SQLAlchemy 1.4 新增此功能
This feature has been added to SQLAlchemy 1.4
https://github.com/sqlalchemy/sqlalchemy/pull/5714
示例:
from sqlalchemy.dialects.mssql import DATETIME2
mssql_type = DATETIME2()
print(type(mssql_type)) # <class 'sqlalchemy.dialects.mssql.base.DATETIME2'>
generic_type = mssql_type.as_generic()
print(type(generic_type)) # <class 'sqlalchemy.sql.sqltypes.DateTime'>
这篇关于如何从供应商特定的数据类型中查找 SQLAlchemy 通用数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!