How to find SQLAlchemy generic datatype from vendor-specific datatype

Viewed 192

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 Answers

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

Related