tommasop
Ecto fragment for array of strings containig an array of strings
I have an Ecto schema with a field that is an array of strings. The underlying database is PostgreSQL.
The field is an array of actually spoken languages, this is the migration definition:
alter table(:users) do
add(:spoken_languages, {:array, :string})
end
and this the schema definition:
schema "user" do
field :email, :string
field :first_name, :string
field :last_name, :string
field :spoken_languages, {:array, :string}
timestamps()
end
I am using JSON:API as a standard and have filters like the following:
http://my_api/users?filter[spoken_languages]=it,es
This postgresql query works:
SELECT t0."id", t0."email", t0."first_name", t0."last_name", t0."spoken_languages", t0."inserted_at", t0."updated_at" FROM "users" AS t0 WHERE
(TRUE AND t0."spoken_languages" @> string_to_array('it,es', ',')::varchar[])
but this Ecto query does not work:
def list_tuners(params) do
Tuner
|> where(^filter_where(params))
|> Repo.all()
end
def filter_where(params) do
Enum.reduce(params["filter"], dynamic(true), fn
{"email", value}, dynamic ->
dynamic([q], ^dynamic and q.email == ^value)
{"spoken_languages", value}, dynamic ->
dynamic(
[q],
^dynamic and
fragment(
"? @> string_to_array(?, ',')::varchar[]",
q.spoken_languages,
^value
)
)
{_, _}, dynamic ->
# Not a where parameter
dynamic
end)
end
This is the generated query:
SELECT t0."id", t0."email", t0."first_name", t0."last_name", t0."spoken_languages", t0."inserted_at", t0."updated_at" FROM "users" AS t0 WHERE (TRUE AND t0."spoken_languages" @> string_to_array($1, ',')::varchar[]) ["it,es"]
It seems like params interpolation is failing because it returns an empty array.
The query seems to be fine and I really don’t have a clue of why it is not working.
Thanks for your help.
Marked As Solved
tommasop
Turns out the query was fine and the problem was in the tests ![]()
Popular in Questions
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New







