westbaystars
Porting a multi-lingual site from using a native XML database to Postgres with JSONB fields, I’m running into problems when I want to filter by a value in the JSON array.
defmodule Myapp.Sports.Person do
use Ash.Resource,
domain: Myapp.Sports,
data_layer: AshPostgres.DataLayer
require Ash.Expr
alias Myapp.Sports.{..., PersonName, ...}
alias Myapp.Calculations.LangEmbeds
...
actions do
...
read :search_with_name do
prepare build(load: [name: expr(%{lang: ^arg(:lang)})])
filter (expr(fragment("to_json(names)::jsonb @@ '($[*].surname like_regex \"^Maki\" flag \"i\") || ($[*].given_name like_regex \"^Maki\" flag \"i\")'")))
argument :name, :string do
allow_nil? false
end
argument :lang, Lang do
allow_nil? false
default :en
end
end
end
...
attributes do
...
attribute :names, {:array, PersonName}, public?: true, allow_nil?: false
...
end
The above read action works in finding all persons with either surname or given_name beginning with “Maki” in the array of PersonName. However, when I change the \"^Maki\" to a ? and pass in ^arg(:name) as both parameters, they don’t appear to be getting inserted into the final query.
filter (expr(fragment("to_json(names)::jsonb @@ '($[*].surname like_regex ? flag \"i\") || ($[*].given_name like_regex ? flag \"i\")'", ^arg(:name), ^arg(:name))))
{:error,
%Ash.Error.Unknown{
bread_crumbs: ["Error returned from: Myapp.Sports.Person.search_with_name"],
query: "#Query<>",
errors: [
%Ash.Error.Unknown.UnknownError{
error: "** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near \"$1\" of jsonpath input\n\n query: SELECT p0.\"attributes\", p0.\"id\", p0.\"links\", p0.\"aliases\", p0.\"names\", p0.\"updated_at\", p0.\"inserted_at\", p0.\"sid\", p0.\"xid\", p0.\"bridges\", p0.\"date_of_birth\", p0.\"date_of_death\", p0.\"drafts\", p0.\"gender\", p0.\"location_of_birth_id\", p0.\"location_of_death_id\", p0.\"stints\" FROM \"persons\" AS p0 WHERE ((to_json(names)::jsonb @@ '($[*].surname like_regex $1 flag \"i\") || ($[*].given_name like_regex $2 flag \"i\")'))",
field: nil,
value: nil,
splode: Ash.Error,
bread_crumbs: ["Error returned from: MyApp.Sports.Person.search_with_name"],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :unknown
}
]
}}
I’ve tried a number of variants of this approach, but none seem to pass the parameters to the fragment.
Any hints would be greatly appreciated.
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
westbaystars
I just realized that I have a
:nameargument and am calculating the:namecalculation field (which is the name for the given:lang). Changing the argument to be:search_name(with appropriatearg(...)updates makes no difference.zachdaniel
The primary issue here is that the whole value after
@@is a single string. The fragment logic is replacing?with the parameter replacements, but postgres doesn’t accept parameters just inside of a string like that.Try replacing your
filterwith this preparation. Note that I’ve also used the expression syntax to interpolate thenamesreference. You should do this instead of putting innamesliterally as that can cause ambiguous queries.westbaystars
Ah, I see. The problem becomes the PSQL side being unable to interpolate the parameters within the
'...'.After putting double quotes around the value being assigned to
jsonpath, there was progress.Revising the
preparestatement like so:I now get the error:
Postgres is now complaining about the
\, expecting them to be followed by a command. They were necessary to escape the quotes on the Elixir side, but appear to now be passed on as-is to the PSQL side.Switching to the PSQL side, I prepared a named statement as:
I wasn’t sure about what type to use to define the
@@portion, so I usedunknownand it figured it out.textdid not work.And with some experimentation, got it to run with:
With that, modifying the generated
SELECTstatement to remove the surrounding double quotes and backslashes, I get:The error returned to the Elixir side is:
I’ve been unable to find the
SELECT {statement} [{params}]syntax in the Postgres documentation. I trust that this creates a temporary prepared statement and executes it. I’ve tried a number of combinations to wrap the parameters (in quotes, back-tics, etc.) with no luck.I feel like this is so very close.
zachdaniel
How about
?
Or perhaps
?::text::unknown?westbaystars
Thank you for the prompt response. However, that also didn’t work.
I’m now looking into how to create a function on the PSQL side that will do do it.
Thank you for your time.
Take care.
westbaystars
This was the clue that I needed to get it to work. I just moved the query building to the Postgres side.
I added a check to make sure that the passed parameter isn’t
NULLor empty, defaulting to searching for all names that begin witha. I’m doing that on the Elixir side as well, so it should never beNULL, but it seems like a good idea to verify either way.Then I call the
person_name_searchfunction in a filter withfragment:I believe that the
search_nameparameter is still being made safe before passing it on. Please correct me if someone thinks not.This is expanded from my original question to include searching the
:aliasesfield, which is also an array ofPersonNameembeds. Once I had this working for:names,oring that with calling it with:aliasesworked as expected.A query through the
Sportsdomain yields:Thank you again for the hint to build the
jsonpathportion separately. That did it.Take care.