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 schema is being defined (in Python). How can I determine this and conditionally choose the data type when the table actually is created on the backend database?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With