4 ms·
but wouldn't that open up possibility of having 2 columns checked? (when in a proper sum type you can't be 2 values at the same time)
by archibaldJ 5y ago
but wouldn't that open up possibility of having 2 columns checked? (when in a proper sum type you can't be 2 values at the same time)
- piaste 5y agoNo, the constraint can require exactly one column. PostgreSQL even has an optimized builtin function for this, `num_nonnulls`. In a recent feature I had an object field modelled as: type Destination = Customer of Customer | Supplier of Supplier | Warehouse of Warehouse and the table representation was customer_id uuid null , supplier_id uuid null , warehouse_id uuid null , constraint unique_destination check (num_nonnulls(customer_id, supplier_id, warehouse_id) = 1) It's not first class support, but manageable enough.