Which belongs to which? (DB, Ecto)

question: which belongs to which?

[product] ?-----? [message]

There is a chatting feature.
and, when sending a message, user can link product to their message
(all messages will be persisted.)

in my brain, each message will have zero or one product link.
so i thought, message has_one product.
however, product shouldn’t belongs_to the message.

so another way is to make message have a product_id and belongs_to the product.
but, it is weird to say, product has_many messages.

I am so confused. any advice would be appreciated.

1 Like

The latter seems to be the way to go. I mean it will be the case that a product “is linked in many” messages. Also you won’t even need to setup a has_many in the product schema if you don’t want/need to.


thank you so much.