tommasop

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

tommasop

Turns out the query was fine and the problem was in the tests :smile:

Where Next?

Popular in Questions Top

_russellb
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
vertexbuffer
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
Tee
can someone please explain to me how Enum.reduce works with maps
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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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

Other popular topics Top

malloryerik
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
vertexbuffer
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
fayddelight
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
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement