moxley
I’m trying to understand the Ash filter expression syntax for working with json arrays within a jsonb column in ash_postgres.
I have a jsonb column, settings, on a table notification_settings
For a simple equality expression, I can use the == operator:
NotificationSettings
|> Ash.Query.filter(settings["foo"] == "bar")
|> Ash.read!(authorize?: false)
But for a value in enum expression:
NotificationSettings
|> Ash.Query.filter("email" in settings["dues_reminders"])
|> Ash.read!(authorize?: false)
It produces:
** (Ash.Error.Unknown)
Bread Crumbs:
> Error returned from: NotificationSettings.read
Unknown Error
* ** (Postgrex.Error) ERROR 22P02 (invalid_text_representation) malformed array literal: "["email"]". If you are trying to query a JSON field, the parameter may need to be interpolated. Instead of
p.json["field"] != "value"
do
p.json["field"] != ^"value"
"[" must introduce explicitly-specified array dimensions.
So I have to use fragment() instead:
NotificationSettings
|> Ash.Query.filter(fragment("?->'dues_reminders' \\? ?", settings, "email"))
|> Ash.read!(authorize?: false)
A very similar thing happens with is_nil() on a jsonb property:
NotificationSettings
|> Ash.Query.filter(is_nil(settings["dues_reminders"]))
|> Ash.read!(authorize?: false)
It produces:
* ** (Postgrex.Error) ERROR 22P02 (invalid_text_representation) malformed array literal: "["email"]"...
For that too, I must use fragment instead:
NotificationSettings
|> Ash.Query.filter(fragment("?->'dues_reminders' is null", settings))
|> Ash.read!(authorize?: false)
Is this correct? Is there no built-in syntax for doing jsonb value comparisons beyond the == operator?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
I believe this is a bug. Please open an issue and I will investigate
zachdaniel
Interesting
I’m getting different behavior.
works, but:
throws a different error:
* ** (Postgrex.Error) ERROR 42809 (wrong_object_type) op ANY/ALL (array) requires array on right sideWhat version of postgres are you on?
moxley
Thanks for looking into this!
Here’s my Postgres version: PostgreSQL 14.17 (Homebrew) on aarch64-apple-darwin24.2.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.6), 64-bit
That version is close to my production database (14.19).
Going off of your example, the SQL generated from the
value in jsonb_column[“array”]case looks something like this:If I set up a test table and record like this:
Then run that query, it produces:
But the critical part is: it only produces an error if there is a record that has a
”dues_reminders”property in the jsonb object. If there’s no record with that property, there’s no error.Additionally, if I remove the
::varchar[]cast at the end of thatjsonb_extract_path_text()expression, it produces a different error:That’s similar to the error you got.
I think maybe
jsonb_extract_path_text(jsonb, txt)::varchar[]is using an incompatible casting (::varchar[]), and’value’ = ANY()isn’t the right expression for this.jsonb_extract_path_text()returns stringified JSON ([“email”]), and the::varchar[]cast wants a PostgreSQL array ({“email”}).This expression works:
I think that’s what ash_postgres needs to generate.
moxley
For the
is_nil(jsonb_column[“property”])case, this SQL works:The difference between this and what ash_postgres is generating is replacing the
::varchar[]cast with::jsonb[].zachdaniel
So, I still haven’t managed to reproduce the issue with
is_nil. Perhaps you could PR a test case toash_postgreswith that behavior? For theincase I’ve fixed that inash_sqlmainmoxley
Thanks @zachdaniel !
I created an ash_postgres PR that reproduces the
is_nil()issue: Demonstrate failed query for is_nil on array within jsonb by moxley · Pull Request #615 · ash-project/ash_postgres · GitHub