A fairly simple use-case: a user has one or more email addresses, stored in the database in 2 separate tables (
emails) such that the
emails table has a foreign key for
user_id. (In reality, my use case is more complex, but this illustrates the problem).
Ecto.Multi lets us create a single transaction for adding both the user record and an email record, and we can rollback if either one of those operations fail. I’m looking at the docs for the
run/5 functions https://hexdocs.pm/ecto/Ecto.Multi.html#run/3 but I can’t figure out how these are supposed to be used. From the docs, I’m not at all clear on what
run function is supposed to return or how that gets ingested by the next operation.
I know I could (sometimes) do this with a single insert operation by leveraging the relationships defined in the schema, but I’m more interested in the principle of the thing.
Thanks as always!