Atomic array removal

Greetings! I'm attempting to remove an element from a string array attribute from all records that have it, but do it atomically with psql's array_remove. I have the following change:
attribute :thing_ids, {:array, :string}, public?: true

update :remove_thing do
argument :thing_id, :string, allow_nil?: false
filter expr(fragment("? = ANY(?)", ^arg(:thing_id), thing_ids))

change atomic_update(
:thing_ids,
expr(fragment("array_remove(?, ?)", ^atomic_ref(:thing_ids), ^arg(:thing_id)))
)
end
attribute :thing_ids, {:array, :string}, public?: true

update :remove_thing do
argument :thing_id, :string, allow_nil?: false
filter expr(fragment("? = ANY(?)", ^arg(:thing_id), thing_ids))

change atomic_update(
:thing_ids,
expr(fragment("array_remove(?, ?)", ^atomic_ref(:thing_ids), ^arg(:thing_id)))
)
end
When I try: Ash.bulk_update!(MyResource, :remove_thing, %{thing_id: thing_id}), it returns the following error: Could not use ":atomic": Cannot atomically update MyResource.thing_ids: Cannot cast a non-literal list atomically Is this just not doable or am I doing it wrong? Thanks
2 Replies
ZachDaniel
ZachDaniel3mo ago
Do atomic_update {:atomic, expr(...)} to bypass the type casting of arrays (which is unnecessary in this case)
bhelms
bhelmsOP3mo ago
Doh. Just adding the option cast_atomic?: false did the trick. Thanks!

Did you find this page helpful?