weljoda

weljoda

Using actor_id in atomic Change causes sql error

Hey there I’m giving Ash my first try and like it pretty well so far. I’m trying to implement a bulk update but run into a SQL error and I can only guess the problem. I have the following Change, similar to the one in the book:

defmodule CourseManager.Chat.Changes.AddUserToSeenBy do
  use Ash.Resource.Change

  @impl true
  def init(opts), do: {:ok, opts}

  @impl true
  def atomic(_changeset, _opts, context) do
    if context.actor do
      actor_id = context.actor.id
      {:atomic,
       %{
         seen_by:
           {:atomic,
            expr(
              fragment(
                "array_prepend(?, array_remove(?, ?))",
                ^actor_id,
                seen_by,
                ^actor_id
              )
            )}
       }}
    else
      :ok
    end
  end
end

And the attribute:

    attribute :seen_by, {:array, :uuid} do
      description "Array of user IDs who have seen this message"
      default []
      public? true
    end

The bulk_update didn’t work, so I tried a single update, but I get the following error:

[debug] QUERY ERROR source="messages" db=1.5ms queue=0.3ms idle=25.3ms
UPDATE "messages" AS m0 SET "updated_at" = (CASE WHEN (array_prepend($1, array_remove(m0."seen_by"::uuid[], $2)))::uuid[] != m0."seen_by"::uuid[] THEN $3::timestamp ELSE m0."updated_at"::timestamp END)::timestamp, "seen_by" = (array_prepend($4, array_remove(m0."seen_by"::uuid[], $5)))::uuid[] WHERE (m0."id"::uuid = $6::uuid) RETURNING m0."id", m0."message", m0."deleted", m0."reactions", m0."seen_by", m0."inserted_at", m0."updated_at", m0."user_id", m0."course_id", m0."parent_message_id" ["71a3f79a-62f6-49c3-8c43-d7d82ab6fd1c", "71a3f79a-62f6-49c3-8c43-d7d82ab6fd1c", ~U[2025-07-28 16:35:28.369137Z], "71a3f79a-62f6-49c3-8c43-d7d82ab6fd1c", "71a3f79a-62f6-49c3-8c43-d7d82ab6fd1c", "b6a170f6-0373-460f-acef-77ae9e76bb72"]

When I put the query in pgadmin and applying the arguments, I only have to use single quotes to make it work correctly.

Is it possible that the PG typecheck fails because the actor_id is not passed as a uuid but as a string instead?
Can anyone give me some advice how to proceed?

Thanks

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Hmm…it could be related to that. You don’t see any other errors? Something you can try as an experiment is transforming the actor_id to a binary before providing it. Or you could try:

type(^actor_id, :uuid) in place of just referencing the actor id.

Also Liked

weljoda

weljoda

Amazing, thanks very much :face_blowing_a_kiss:. It did the trick.

              fragment(
                "array_prepend(?, array_remove(?, ?))",
                type(^actor_id, :uuid),
                seen_by,
                type(^actor_id, :uuid)
              )

Last Post!

weljoda

weljoda

Amazing, thanks very much :face_blowing_a_kiss:. It did the trick.

              fragment(
                "array_prepend(?, array_remove(?, ?))",
                type(^actor_id, :uuid),
                seen_by,
                type(^actor_id, :uuid)
              )

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

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 54092 488
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement