Hi fellas,
It’s my first time asking something here, here it is:
I’ve checked couple of resources (here and the official docs), yet could not succeeded on writing a proper query for searcing an item on an array field (genres).
Below works,
iex(21)> Ecto.Adapters.SQL.query(ArtistsApp.Repo, "select * from musicians where array_to_string(genres, ',') like 'Alter%'")
[debug] QUERY OK db=1.5ms queue=3.4ms idle=1035.1ms
select * from musicians where array_to_string(genres, ',') like '%lternative%' []
{:ok,
%Postgrex.Result{
columns: ["id", "name", "discogs_id", "details", "countries", "genres",
"external_urls", "inserted_at", "updated_at", "image_cover",
"image_thumb"],
command: :select,
connection_id: 31704,
messages: [],
num_rows: 1,
rows: [
[
1,
"Gaye Su Akyol",
123123,
"dasdasd",
["'Turkey'"],
["'Rock'", "'Alternative'"],
nil,
~N[2020-09-10 15:10:30.000000],
~N[2020-09-10 15:10:30.000000],
nil,
nil
]
However when I want to query with variable, it returns zero rows.
Ecto.Adapters.SQL.query(ArtistsApp.Repo, “select * from musicians where array_to_string(genres, ‘,’) like ‘$1’”, [“Alter” <> “%”])
So far I’ve tried with like, ILIKE and without quotes around $1 to experiment. But they’ve failed.
Could you help me on how to make it work?
Thank you very much!