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
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
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
can someone please explain to me how Enum.reduce works with maps
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
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
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
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
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









