jan-mb-me

jan-mb-me

Ash Postgres: How to detect change when updating nullable column from NULL to non-NULL value without using atomic_update?

Hi,

I stumbled over this snippet of a query generated by an update action:

SET “updated_at” = (CASE WHEN $1::timestamp != r0.“last_query_at”::timestamp THEN $2::timestamp ELSE r0.“updated_at”::timestamp END)

The attribute is defined as below, i.e. the column is nullable:

attribute :last_query_at, :utc_datetime_usec do
  public? true
  filterable? true
end

When the record/row has NULL in field/column last_query_at and I pass a date-time value for last_query_at to the update action, the operator != prevents the condition to resolve to TRUE, hence updated_at is not changed although the row is changed to a new value for last_query_at (not shown in the snippet above).

That default behavior caught me by surprise.

From what I found so far, I‘d have to use this in the update action to skip the “only update if changed“ optimization:
change atomic_update(:last_query_at, expr(^arg(:last_query_at)))

Am I missing something to make the update of updated_at work without atomic_update?

I am using ash 3.19.3 and ash-postgres 2.6.32.

Update: Using atomic_update leads to the same snippet being generated.

Marked As Solved

jan-mb-me

jan-mb-me

Hello @zachdaniel, Upgrading to ash 3.23.0+ and also upgrading ash_sql worked and my workaround became obsolete.
Thank you very much!

Also Liked

zachdaniel

zachdaniel

Creator of Ash

Hey @jan-mb-me thank you for the find! This is fixed in the latest main of ash and will be included in the next release.

:person_bowing:

zachdaniel

zachdaniel

Creator of Ash

Release is available: 3.23.0

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement