Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)


No, 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.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: