Is there a good way to handle sum types (or tagged unions) in PostgreSQL?

2023-05-18 Thread Victor Nordam Suadicani
Hi, Is there any nice way to handle sum types (aka tagged unions) in a PostgreSQL database? I've searched far and wide and have not reached any satisfying answer. As a (somewhat contrived) example, say I have the following enum in Rust: enum TaggedUnion { Variant1(String), Variant2(i32),

Re: Is there a good way to handle sum types (or tagged unions) in PostgreSQL?

2023-05-18 Thread Adrian Klaver
On 5/18/23 05:27, Victor Nordam Suadicani wrote: Hi, Is there any nice way to handle sum types (aka tagged unions) in a PostgreSQL database? I've searched far and wide and have not reached any satisfying answer. As a (somewhat contrived) example, say I have the following enum in Rust: enum

Re: Is there a good way to handle sum types (or tagged unions) in PostgreSQL?

2023-05-18 Thread Victor Nordam Suadicani
A composite type is a *product type* , not a sum type . PostgreSQL currently has great support for product types, but basically no support for sum types. >From the perspective of algebraic data types, this feels

Re: Is there a good way to handle sum types (or tagged unions) in PostgreSQL?

2023-05-19 Thread Dominique Devienne
On Thu, May 18, 2023 at 2:28 PM Victor Nordam Suadicani < v.n.suadic...@gmail.com> wrote: > Is there any nice way to handle sum types (aka tagged unions) in a > PostgreSQL database? [...] > A third method would be to save all fields of all variants into a single > table, with all fields being null

Re: Is there a good way to handle sum types (or tagged unions) in PostgreSQL?

2023-05-19 Thread Victor Nordam Suadicani
On Fri, 19 May 2023 at 12:44, Dominique Devienne wrote: > On Thu, May 18, 2023 at 2:28 PM Victor Nordam Suadicani < > v.n.suadic...@gmail.com> wrote: > >> Is there any nice way to handle sum types (aka tagged unions) in a >> PostgreSQL database? [...] >> > A third method would be to save all fiel

Re: Is there a good way to handle sum types (or tagged unions) in PostgreSQL?

2023-05-19 Thread Peter J. Holzer
On 2023-05-19 16:55:00 +0200, Victor Nordam Suadicani wrote: > Thanks for the perspective :) > > > If you need SQL, you need to design for SQL for the get go. Not shoehorn > > your > > Rust data model into SQL. > > Sometimes the data in the domain really does fit a sum type and then a sum > ty