How to find SQLAlchemy generic datatype from vendor-specific datatype

亡梦爱人 提交于 2021-01-04 07:01:05

问题


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?

For instance, if I have the vendor-specific type sqlalchemy.dialects.postgresql.INTEGER, I would want to map back to sqlalchemy.sql.sqltypes.Integer.

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.


回答1:


This feature has been added to SQLAlchemy 1.4

https://github.com/sqlalchemy/sqlalchemy/pull/5714

Example:

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'>



来源:https://stackoverflow.com/questions/64214225/how-to-find-sqlalchemy-generic-datatype-from-vendor-specific-datatype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!