Handle_event updating the wrong record

I have this code snippet from the getting started guide for ash_phoenix:
Repo GitHub - albinkc/my_ash_phoenix_app_tut

def handle_event("update_post", %{"form" => form_params}, socket) do
    case AshPhoenix.Form.submit(socket.assigns.update_form, params: form_params) do
      {:ok, _post} ->
        posts = Blog.list_posts!()

        {:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}

      {:error, update_form} ->
        {:noreply, assign(socket, update_form: update_form)}
    end
  end
[debug] HANDLE EVENT "update_post" in MyAshPhoenixAppWeb.PostsLive
  Parameters: %{"form" => %{"content" => "uh", "post_id" => "ae34b684-7308-42eb-8ea1-f0455fcb66a6"}}
[debug] QUERY OK source="posts" db=2.3ms idle=1555.3ms
UPDATE "posts" AS p0 SET "content" = $1::text::text WHERE (p0."id"::uuid = $2::uuid) RETURNING p0."id", p0."title", p0."content" ["uh", "f38c0f31-42f8-4bce-847c-ec3dd3593d6b"]
↳ AshPostgres.DataLayer.update_query/4, at: lib/data_layer.ex:1380
[debug] QUERY OK source="posts" db=0.6ms idle=1561.4ms
SELECT p0."id", p0."title", p0."content" FROM "posts" AS p0 []
↳ anonymous fn/3 in AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:767
[debug] Replied in 12ms

notice the uuids are different in the form_params vs the postgres statement. Furthermore, if I delete all but one record from the db table and then try the update, I get this cryptic warning. No db update occurs in this case.

[debug] HANDLE EVENT "update_post" in MyAshPhoenixAppWeb.PostsLive
  Parameters: %{"form" => %{"content" => "hehe", "post_id" => "43c61b5a-18d4-4c74-9063-2c90fee59724"}}
[debug] QUERY OK source="posts" db=1.0ms idle=1857.1ms
UPDATE "posts" AS p0 SET "content" = $1::text::text WHERE (p0."id"::uuid = $2::uuid) RETURNING p0."id", p0."title", p0."content" ["hehe", "f38c0f31-42f8-4bce-847c-ec3dd3593d6b"]
↳ AshPostgres.DataLayer.update_query/4, at: lib/data_layer.ex:1380
warning: the following fields are unknown when raising Ash.Error.Changes.StaleRecord: [filters: %{id: "f38c0f31-42f8-4bce-847c-ec3dd3593d6b"}]. Please make sure to only give known fields when raising or redefine Ash.Error.Changes.StaleRecord.exception/1 to discard unknown fields. Future Elixir versions will raise on unknown fields given to raise/2
  (ash 3.3.3) lib/ash/error/changes/stale_record.ex:5: Ash.Error.Changes.StaleRecord."exception (overridable 1)"/1
  (ash 3.3.3) lib/ash/error/changes/stale_record.ex:5: Ash.Error.Changes.StaleRecord.exception/1
  (ash 3.3.3) lib/ash/actions/update/update.ex:183: Ash.Actions.Update.run/4
  (ash 3.3.3) lib/ash.ex:2502: Ash.update/3
  (ash_phoenix 2.1.1) lib/ash_phoenix/form/form.ex:2043: AshPhoenix.Form.with_changeset/2
  (ash_phoenix 2.1.1) lib/ash_phoenix/form/form.ex:1861: AshPhoenix.Form.submit/2
  (ash_phoenix 2.1.1) lib/ash_phoenix/form/form.ex:1790: AshPhoenix.Form.submit/2
  (my_ash_phoenix_app 0.1.0) lib/my_ash_phoenix_app_web/posts_live.ex:79: MyAshPhoenixAppWeb.PostsLive.handle_event/3
  (phoenix_live_view 1.0.0-rc.6) lib/phoenix_live_view/channel.ex:508: anonymous fn/3 in Phoenix.LiveView.Channel.view_handle_event/3
  (telemetry 1.2.1) /Users/albin/projects/ash_builder/my_ash_phoenix_app/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
  (phoenix_live_view 1.0.0-rc.6) lib/phoenix_live_view/channel.ex:260: Phoenix.LiveView.Channel.handle_info/2
  (stdlib 6.0) gen_server.erl:2173: :gen_server.try_handle_info/3
  (stdlib 6.0) gen_server.erl:2261: :gen_server.handle_msg/6
  (stdlib 6.0) proc_lib.erl:340: :proc_lib.wake_up/3

[warning] Unhandled error in form submission for MyAshPhoenixApp.Blog.Post.update

This error was unhandled because Ash.Error.Changes.StaleRecord does not implement the `AshPhoenix.FormData.Error` protocol.

** (Ash.Error.Changes.StaleRecord) Attempted to update stale record of MyAshPhoenixApp.Blog.Post

[debug] Replied in 4ms

how can I make this work? TIA

The record to be updated does not come from form params but from what is passed in to create the form. What are you passing into AshPhoenix.Form.for_update?

Looks like you’re always updating the first post in the list regardless of params: my_ash_phoenix_app_tut/lib/my_ash_phoenix_app_web/posts_live.ex at 028ad59eb91989d16d81cf758a76f858db86f2b6 · albinkc/my_ash_phoenix_app_tut · GitHub

To change what you’re updating, you’ll need to replace the form with a new form.

1 Like

Thank you for taking the time! That should work.