python - how to define a unique constraint depending on column value in postgresql? -


i have transaction table defined follows:

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')) 

there 2 types of transactions. type='1', have unique constraint above i.e cust_id, date. transactions of type '2', there can more 1 type of transactions same date. how define such constraint? using sqlalchemy core 1.0.8, python 2.7, postgresql 9.6

you can have functional unique indices:

index("ix_transaction_type_1", mtransactions.c.cust_id, mtransactions.c.date, postgresql_where=mtransactions.c.type = "1") 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -