benonymus

benonymus

Unexpected result in mid-multi query

Hey,

Here is my little multi:

    case attrs do
      %{"default" => "true"} ->
        Multi.new()
        |> Multi.update_all(:set_default_to_false, VATType, set: [default: false])

      _ ->
        Multi.new()
    end
    |> Multi.update(:update_vat_type, VATType.changeset(vat_type, attrs))
    |> Multi.run(:no_default, fn repo, _ ->
      IO.inspect(get_by_id(vat_type.id))

      case repo.get_by(VATType, default: true) do
        %VATType{} = _vat_type ->
          {:ok, :present}

        x ->
          IO.inspect(x)
          {:error, VATType.changeset_error(:default, "no default", vat_type)}
      end
    end)
    |> Repo.transaction()

What confuses me is the following:
I update all the items’ default value to false, and then I update the current one as default, to ensure only one is default at any time.
When I get the item to do the check in the run, what state am I getting?
It is not the db, because in the db I am seeing true and here false.
But if I am setting from false to true I am still seeing false here.

Could someone shed some light on this?
The weird behavior happens once my update fails and I retry.

I thought that queries inside the multi return the state inside the multi, is this correct?
With this multi shouldn’t I be getting the result of the single update always?

EDIT:
If I explicitly omit the item in question from the update_all step then it works as expected.

Marked As Solved

al2o3cr

al2o3cr

What’s the value in vat_type here? It’s read during the construction of the multi, before the update_all, so if attrs has %{"default" => "true"} but vat_type.default is already true then update_vat_type will be a no-op.

Last Post!

benonymus

benonymus

Yes, if I change the update to run and get the vat_type in there, after the update_all it is false and gets updated, instead of no-op.

Thank you

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement