Upsert a many-to-many relation using Ecto

Hello,

I am trying to upsert a many to many relation with Ecto.

The functionality I am looking for is this:

INSERT INTO company_memberships (account_id, company_id, membership_type) VALUES (2, 17, 'owner')
ON CONFLICT (account_id, company_id)
DO UPDATE SET membership_type=EXCLUDED.membership_type;

I see in the Ecto documentation the following:

Because of the inability to know if the struct is up to date or not, inserting a struct with associations and using the :on_conflict option at the same time is not recommended, as Ecto will be unable to actually track the proper status of the association

Is there a recommended way to do this?

Thanks!

1 Like