Attempted to update stale record error update record in a loop

I think it is from ecto and i miss something, i do not want to use Ash generator here! so with a sleep it returns error
** (Ash.Error.Invalid)
Bread Crumbs:
> Error returned from: MishkaCms.Runtime.Layout.update

Invalid Error

* Attempted to update stale record of MishkaCms.Runtime.Layout with filter: site_id == "77fb97f0-2b9f-4831-aadd-b6a956ad0ead
** (Ash.Error.Invalid)
Bread Crumbs:
> Error returned from: MishkaCms.Runtime.Layout.update

Invalid Error

* Attempted to update stale record of MishkaCms.Runtime.Layout with filter: site_id == "77fb97f0-2b9f-4831-aadd-b6a956ad0ead
Simple Code:
layout_id = layout.id

Enum.each(1..25, fn item ->
fresh_layout = Ash.get!(MishkaCms.Runtime.Layout, layout_id, action: :get_any, authorize?: false)

fresh_layout
|> Ash.Changeset.for_update(:update, %{
description: "Simple main layout with navigation V#{item}"
})
|> Ash.update!(tenant: site.id, actor: none_user, authorize?: false, atomic_upgrade?: false)

Process.sleep(300)
end)
layout_id = layout.id

Enum.each(1..25, fn item ->
fresh_layout = Ash.get!(MishkaCms.Runtime.Layout, layout_id, action: :get_any, authorize?: false)

fresh_layout
|> Ash.Changeset.for_update(:update, %{
description: "Simple main layout with navigation V#{item}"
})
|> Ash.update!(tenant: site.id, actor: none_user, authorize?: false, atomic_upgrade?: false)

Process.sleep(300)
end)
What am i missing? every time i get record again 🤔 thanks
Solution:
I wrapped it inside, i think it fixed my code 🧑‍💻 ``` Ash.DataLayer.transaction(MishkaCms.Runtime.Layout, fn -> .... end)...
Jump to solution
1 Reply
Solution
Shahryar
Shahryar2w ago
I wrapped it inside, i think it fixed my code 🧑‍💻
Ash.DataLayer.transaction(MishkaCms.Runtime.Layout, fn ->
....
end)
Ash.DataLayer.transaction(MishkaCms.Runtime.Layout, fn ->
....
end)

Did you find this page helpful?