3 ms·
As the parent mentioned, you can encode your sum type by providing all the columns and constraining exactly one to be non-null.
by gpderetta 5y ago
As the parent mentioned, you can encode your sum type by providing all the columns and constraining exactly one to be non-null.
- occamrazor 5y agoThat’s the most sensible solution. AFAIK however there neither a cross-dialect way to specify that specific constraint, nor a simple way to SELECT the column name and the value of the unique non-null value.
- archibaldJ 5y agobut 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.
- naasking 5y agoYes, you can encode it, but you shouldn't have to invent and apply this encoding manually. It's error-prone, it's less efficient, and you lose information. It's like saying you don't need foreign key constraints as built-in concept as long as you have triggers, because you can encode such integrity checks as triggers. Technically true, but no one is going to buy that is a legitimate argument against foreign key constraints.
- gpderetta 5y agoI guess what I wanted to say is that there is nothing in the relational model per se that prevents encoding sum types. Of course actual implementations (i.e. SQL) might lack the required syntactic sugar.
- naasking 5y agoI agree, I'm just pointing out that when you have some abstraction X you almost always want to also provide its categorical dual ~X, or you end up having to write awkward encodings to simulate it. Databases provide product types, and the dual of product types are sum types. As you point out, you can encode this in various ways, but it's not natural and very error-prone.