benonymus

benonymus

Hey there,

I have the relatively simple Multi:

  def set_user_team_is_default(%UserTeam{user_id: user_id, team_id: team_id}) do
    Multi.new()
    |> Multi.update_all(
      :set_other_teams_to_non_default,
      from(ut in UserTeam, where: ut.user_id == ^user_id and ut.is_default == true),
      set: [is_default: false]
    )
    |> Multi.update(
      :set_to_default,
       # getting the user_team here or not getting and using the received one results in the same
      UserTeam.changeset(get_user_team(user_id, team_id), %{is_default: true})
    )
    |> Repo.transaction()
  end

It is used to set every user_team(that is default) is_default to false and then set one as default.

Now the following unexpected thing happened:

  1. User opened the page in browser A and then opened the same page in browser B at the same time.

  2. User selects team A as default in browser A - works as expected

  3. User selects same team A as default in browser B - user ends up without a default team.

Why does this happen?

The result of the multi when this occurs is this:

{:ok,
 %{
   set_other_teams_to_non_default: {1, nil},
   set_to_default: %XXX.UsersTeams.UserTeam{
     __meta__: #Ecto.Schema.Metadata<:loaded, "users_teams">,
     id: 3,
     inserted_at: ~N[2022-03-01 09:28:23],
     is_default: true,
     role: :admin,
     status: :active,
     team: #Ecto.Association.NotLoaded<association :team is not loaded>,
     team_id: 3,
     updated_at: ~N[2022-04-07 05:54:59],
     user: #Ecto.Association.NotLoaded<association :user is not loaded>,
     user_id: 2
   }
 }}

As you see in the result of the multi is_default is true for the user_team.
But!
If I fetch the user_team right after the multi it shows this:

%XXX.UsersTeams.UserTeam{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users_teams">,
  id: 3,
  inserted_at: ~N[2022-03-01 09:28:23],
  is_default: false,
  role: :admin,
  status: :active,
  team: #Ecto.Association.NotLoaded<association :team is not loaded>,
  team_id: 3,
  updated_at: ~N[2022-04-07 05:54:59],
  user: #Ecto.Association.NotLoaded<association :user is not loaded>,
  user_id: 2
}

While the multi returns that is_default is true, in the database it is false!

According to the multi docs: Ecto.Multi — Ecto v3.14.0

If a multi is valid (i.e. all the changesets in it are valid), all operations will be executed in the order they were added.

Can someone explain as to why does this happen?

Fixing it is easy(patter match on the incoming user_team, if is_default: true ignore it), I am not asking for that, I want to understand why does it happen.

Showing Posts 1 to 6

LostKobrakai

LostKobrakai

Multi.update / Repo.update is considered a noop (no query to the db) if there are no changes on the schema. If you have a user_team, which is the default, and then add a change of making it the default, then this is such a case of “no change present”. So you might not always get two queries here. It might be just one.

This doesn’t set “other” teams to not be defaults. This unsets all default teams not matter if it’s the current one or not.

What I can’t understand is why doing get_user_team(user_id, team_id) in the second step didn’t help resolve issues. Nothing should be a default, so making the current userteam the default should always work.

benonymus

benonymus OP

Correct, step 1 will set all default teams to not default, even the sent one if it is the default(in the actual code the default false setting is a reusable multi piece, I don’t think step 1 should care about if the user_team you are sending is a default or not).

Step 2 tries to set the sent one as the default(which I expected to be set as default even if it was set to false in step one, as you said.)

LostKobrakai

LostKobrakai

Step 2 only sets the default if the base data for the changeset have the user_team not be the default already (unless you use force_change). Otherwise the changeset sees a value to be set to the value already on the schema, which makes step 2 a noop. Not sure if that’s happening here / for you though.

benonymus

benonymus OP

Something interesting:
If I change step 2 to be like this:

    |> Multi.update(:set_to_default, fn _ ->
      UserTeam.changeset(get_user_team(user_id, team_id), %{is_default: true})
    end)

It works as expected, always.

benonymus

benonymus OP

If I do this(back to the original):

    |> Multi.update(
      :set_to_default,
      UserTeam.changeset(get_user_team(user_id, team_id) |> IO.inspect(), %{is_default: true})
    )

In the output it shows the is_default as true.

%XXX.UsersTeams.UserTeam{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users_teams">,
  id: 157,
  inserted_at: ~N[2022-03-18 08:20:38],
  is_default: true,
  role: :owner,
  status: :active,
  team: #Ecto.Association.NotLoaded<association :team is not loaded>,
  team_id: 76,
  updated_at: ~N[2022-04-07 09:17:03],
  user: #Ecto.Association.NotLoaded<association :user is not loaded>,
  user_id: 2
}

So it will be a noop as you said.

But then does that mean that this get_user_team runs outside the transaction?

Edit: Looks like yes, looking at it a bit more it makes sense. If you rely on changes in the transaction, that part needs to run IN the transaction in the fn. The docs more or less show this, but don’t say it explicitly.

Thanks @LostKobrakai for facilitating the thinking process!

LostKobrakai

LostKobrakai

Yes. I didn’t notice that. Without the anonymous function the code runs when defining the step, rather then when executing the step within the transaction.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews