且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLAlchemy:如何根据列的后端有条件地选择列的类型

更新时间:2022-03-04 02:23:57

您可以通过

You can accomplish something like this with TypeEngine.with_variant:

from sqlalchemy.types import PickleType
from sqlalchemy.dialects import postgresql

HybridType = PickleType()

HybridType = HybridType.with_variant(postgresql.HSTORE(), 'postgresql')

这会创建一个新类型HybridType,您可以像使用其他任何类型一样使用它,但要注意的是它将在Postgres上生成一个HSTORE列,并在其他任何地方生成一个PickleType.

This creates a new type, HybridType, which you can use like any other type, with the caveat that it will produce an HSTORE column on Postgres and a PickleType everywhere else.