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?
Asked
Active
Viewed 2,542 times
13

minhee
- 5,688
- 5
- 43
- 81
1 Answers
16
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.

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

Kurt Raschke
- 960
- 9
- 19