我的事务表定义如下:
mtransactions = Table("mtransactions", metadata,
Column("id", Integer, primary_key=True),
Column("type", String, default='Reg'),
Column("cust_id", Integer, ForeignKey("b_customers.id")),
Column("date", Date,
default=datetime.datetime.utcnow().date()),
UniqueConstraint('cust_id','date', name='cust_date_tran'))
只有两种类型的交易。对于type =' 1',我希望有如上所述的唯一约束,即cust_id,date。但对于类型为' 2'的交易,同一日期可能有多种类型的交易。我如何定义这样的约束?仅使用sqlalchemy核心1.0.8,python 2.7,postgresql 9.6
答案 0 :(得分:2)
Index("ix_transaction_type_1", mtransactions.c.cust_id, mtransactions.c.date, postgresql_where=mtransactions.c.type = "1")