I have a table (TA) on which I want to have a composite unique index on two columns (col1 and col2).
col2 can be NULL.
I know Postgres treats NULLs as distinct.
In SQL I would therefore do:
CREATE UNIQUE INDEX index_name ON TA (col1, col2) NULLS NOT DISTINCT
How can I achieve the same in a SQLAlchemy model?
Something like:
class TherapeuticArea(Base):
__tablename__ = "TA"
__table_args__ = (
sa.Index(
"index_name",
"col1",
"col2",
unique=True,
# postgresql_ option here? Which one? Or..?
),
)
Note: so far I have added such SQL statement as custom op.execute() to the migration generated by Alembic and it works fine.
But then Alembic keeps removing the index on subsequent migrations.
Therefore I'd like to have it as a plain declaration in the model