francois

francois

How to run a SELECT WHERE column IN (...) query?

Hi all!

I don’t know what to do to make Ecto happy to run the following query:

 sql = "" <>
   "SELECT chat_user_notification_keys.key " <>
   "FROM chat_user_rooms " <>
   "JOIN chat_user_notification_keys ON chat_user_notification_keys.user_id = chat_user_rooms.user_id " <>
   "JOIN users                       ON users.id = chat_user_rooms.user_id " <>
   "JOIN chat_rooms                  ON chat_rooms.id = chat_user_rooms.room_id " <>
   "WHERE chat_rooms.id_ref = $1::text::uuid " <>
   "  AND users.slug NOT IN ($2)"

 case Ecto.Adapters.SQL.query(BusChatWeb.Repo, sql, [room_id, user_slugs_who_are_present]) do
   {:ok, %{num_rows: num_keys, rows: keys}} ->
     case num_keys do
       0 -> # ...
       _ -> # ...
     end
 end

Currently, I get the following error message:

** (ArgumentError) Postgrex expected a binary, got []. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
[debug] QUERY ERROR db=9.9ms
SELECT chat_user_notification_keys.key FROM chat_user_rooms JOIN chat_user_notification_keys ON chat_user_notification_keys.user_id = chat_user_rooms.user_id JOIN users                       ON users.id = chat_user_rooms.user_id JOIN chat_rooms                  ON    chat_rooms.id = chat_user_rooms.room_id WHERE chat_rooms.id_ref = $1::text::uuid   AND users.slug NOT IN ($2) ["afe13f89-7819-43d9-9bb8-b4ba38a2ddac", []]
            (ecto) /Users/francois/Projects/chat_web/deps/postgrex/lib/postgrex/type_module.ex:717: Ecto.Adapters.Postgres.TypeModule.encode_params/3
        (postgrex) lib/postgrex/query.ex:45: DBConnection.Query.Postgrex.Query.encode/3
    (db_connection) lib/db_connection.ex:1071: DBConnection.describe_run/5
    (db_connection) lib/db_connection.ex:1142: anonymous fn/4 in DBConnection.run_meter/5
    (db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
            (ecto) lib/ecto/adapters/postgres/connection.ex:93: Ecto.Adapters.Postgres.Connection.execute/4
            (ecto) lib/ecto/adapters/sql.ex:243: Ecto.Adapters.SQL.sql_call/6
        (chat_web) lib/chat_web/repo.ex:117: ChatWeb.Repo.find_notifiable_keys/2

The problem is related to the $2 parameter: it’s a list in Elixir-land. How can I convert/cast a list to something Ecto will be happy?

Thanks!

Most Liked

francois

francois

Thanks @OvermindDL1. The UUIDs are coming straight from the UI layer. I kept them as strings, because that makes it easy to inspect the value from anywhere in the stack.

I finally found a solution to my problem by reusing the ANY operator. My original SQL query is now:

-- as before
  AND NOT users.slug = ANY ($2::text[])

This works and satisfies me.

Thanks!

zambal

zambal

You need to dump the string UUID first to a binary like this:

{:ok, bin} = Ecto.UUID.load(room_id)

Now you can use the binary UUID directly in your query like this:

where: chat_rooms.id_ref == ^bin

Note that if you use an Ecto schema, you don’t need to do all this manual casting and dumping.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
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 I...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
joaquinalcerro
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 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
josevalim
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement