4 ms·
Thinking a bit more about it I think this would be a very elegant solution for Tagged-Unions in SQL: - simply allow tagged unions for foreign key constraints:
by laszlokorte 5y ago
Thinking a bit more about it I think this would be a very elegant solution for Tagged-Unions in SQL:
- simply allow tagged unions for foreign key constraints:
CONSTRAINT my_fk FOREIGN KEY (own_column) REFERENCES EITHER table_a (a_id) OR table_b (b_id) OR table_c (c_id)
- add join syntax to join via defined foreign key:
FROM own_table
JOIN VIA my_fk
The union FK would store an additional flag determining the target table
and the JOIN VIA would switch on that flag and it would be allowed to use any columns from all target tables in the query but only the matching ones would be not null.
- goto11 5y agoIf you put the foreign keys on the table_a/b/c tables, then you can express the same without having to introduce tagged unions.
- laszlokorte 5y agoAs I said in response to a sibling: But can you can not (in a simple way) prevent that both a circle and a triangle point to the same user and you can not enforce a NOT NULL for a user to must have a favourite shape.