SQLAlchemy: How to conditionally choose type for column by depending on its backend

前端 未结 1 1729
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-20 13:52

I want to use HSTORE type for a column if it uses PostgreSQL as its backend, or PickleType otherwise. The problem is that we cannot determine which backend will be used when sc

相关标签:
1条回答
  • 2021-02-20 14:37

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

    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.

    0 讨论(0)
提交回复
热议问题