albin

albin

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

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

First 3 of 3 Posts Switch mode

zachdaniel

zachdaniel

Creator of Ash

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?

zachdaniel

zachdaniel

Creator of Ash

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.

albin

albin OP

Thank you for taking the time! That should work.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement