How to do polymorphic relationships, but with only 1 table?

Apologies if this has been answered before, but I couldn't find it in the search function. I want to create a table called transactions with the following columns:
transactions
------------
source_id
source_type
amount
destination_id
destination_type
transactions
------------
source_id
source_type
amount
destination_id
destination_type
I know there is documentation regarding polymorphic resources in Ash, but it uses multiple tables to create the relationship. My intention with the transactions table is to be able to do various combinations of source_type and destination_type, so I don't think having multiple tables is a good solution. How can I do this in Ash?
3 Replies
ZachDaniel
ZachDaniel•3y ago
So you can do this, but (just like in any other set of tooling) its not necessarily going to be easy The way that you do it is with calculations that produce union types but keep in mind, this design pattern means you can't have foreign keys Assuming destination_id could point at one of many tables
Terryble
TerrybleOP•3y ago
Thanks! Looks like I have to give it some more thought then
kernel
kernel•3y ago
I'd recommend doing it as multiple tables also - foreign keys are very nice to have 🙂

Did you find this page helpful?