How can I represent a discriminated union Relationship in SQLModel/SQLAlchemy?
Consider a database with three tables, Parent
, ChildTypeA
, and ChildTypeB
. Parent
has the fields id
, and child_type
(an ENUM
of “a” and “b”), and a unique constraint over both fields. ChildTypeA
and ChildTypeB
both have the field supertype_key
(the aforementioned ENUM
), and parent_id
. They also have a composite unique foreign key of (parent_id, supertype_key)
to (Parent.id, Parent.child_type)
.