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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
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
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
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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 54006 488
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement