How to make one object have 2 different relationships with another object.
I have 2 tables
Product
and Media
. The product has a thumbnail and also images of the product. The thumbnail is one media
and images are many media
.
So in short I have to have one-to-one
relation between Product.thumbnail
and Media
, and a one-to-many
relation between Product.images
and Media
.
My code follows the docs about relations but it does not work (my guess because of circular references).
These lines cause a TS error:
How do I correctly implement 2 different relations between 2 tables?9 Replies
bump
https://drizzle.run/ryxw1u9ieaf45i9owbpwbw70
It should do what you expect
Note that you will have the thumnail in images too. If you want to discard it for some reason, you should add a discriminent column (like a type)
this seems a bit too complicated is my approach good for this?
with a discriminent on media type: https://drizzle.run/kjm4cv2t22iwmzdcmmnhcc3y
What do you find complicated? The example or adding a more complexity filtering images and thumbnail?
the relationship I am building here. I haven't seen anything like it in the docs and the ai on the website couldn't build it properly. maybe my approach from the start of creating this is wrong.
no it is good. We lack example because we can't cover every use cases
if you look at what you tried and the playground, it is exactly what you did + some fixes not obvious just reading the doc
okay thank you. I will study the examples you provided me
the
images
(many) and productImages
(one) need the same relation name to glue all the thing
+ some AnyPgColumn
fro typescript
If you want more control, you can also have a separate table for thumbnails. Just a matter of choicethis might actually be what I do in the end.