在Postgres上用sqlalchemy创建部分唯一索引

2024-04-23 16:44:36 发布

您现在位置:Python中文网/ 问答频道 /正文

SQLAlchemy支持创建partial indexes in postgresql

是否可以通过SQLAlchemy创建partial unique index

假设一个表/模型是这样的:

class ScheduledPayment(Base):
     invoice_id = Column(Integer)
     is_canceled = Column(Boolean, default=False)

我想要一个唯一的索引,其中只能有一个“活动”的时间表为给定的发票付款。

我可以在postgres中手动创建:

CREATE UNIQUE INDEX only_one_active_invoice on scheduled_payment 
     (invoice_id, is_canceled) where not is_canceled;

我想知道如何使用SQLAlchemy 0.9将其添加到我的SQLAlchemy模型中。


Tags: in模型idindexsqlalchemyispostgresqlcolumn