foundart

foundart

Transactions- what are the basics?

I have a situation similar to create and insert 2 associated models.

In my case User has_many Others and Others belongs_to User.
I want to insert a user and an Other in one transaction.

I’m doing a put_assoc from User and it works fine. It generates 2 insert statements of course.

My question is: Do I need to wrap the insert in a Repo.transaction? I have been unable to find any documentation that talks about how the inserts are handled. If I did not wrap the insert function in a transaction, would the 2 sql statements be batched in a single transaction by default or would they be done as 2 separate transactions?

  MyModule.Repo.transaction fn ->
    MyModule.Repo.insert(changeset)
  end

Most Liked

szsoppa

szsoppa

If anyone coming to this topic needs more tips on Ecto.Multi and transactions in general, we’ve written a post about it: How database transactions work in Ecto and why Elixir makes it awesome? :wink:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yes, you can observe this with debug logging:

iex(5)> %Menu.Item{name: "Bar", price: 4.50, category: %Menu.Category{name: "Foo"}} |> Repo.insert!
[debug] QUERY OK db=0.3ms
begin []
[debug] QUERY OK db=6.6ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" ["Foo", {{2017, 11, 3}, {20, 21, 34, 814485}}, {{2017, 11, 3}, {20, 21, 34, 817077}}]
[debug] QUERY OK db=5.0ms
INSERT INTO "items" ("category_id","name","price","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" [4, "Bar", #Decimal<4.5>, {{2017, 11, 3}, {20, 21, 34, 834005}}, {{2017, 11, 3}, {20, 21, 34, 834009}}]
[debug] QUERY OK db=0.8ms
commit []

Note the begin and commit lines. Those are the beginning and end of the transaction respectively.

theangryangel

theangryangel

You probably want an Ecto.Multi rather than using transactions directly as you get a bunch of extra stuff that you don’t get with Repo.transaction

Last Post!

Zahi

Zahi

Seems like the site moved (your link is broken). Here’s one that works.

Thanks for the article.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement