francois
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!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
francois
I tried using the Ecto.Query API, like this:
This fails because room_id is a string, but the database type is a UUID. The above fails with:
If I remove the type cast, then Postgrex complains with:
Note that the above query is not even the same query I wrote originally. I used
NOT IN, but this query isIN. I have to find how to do aNOT INnow.Thanks!
OvermindDL1
not users.slug in ^user_slugs_who_are_present, thenotis a unary function, theinis a binary function, thus the precedence is likenot(users.slug in ^user_slugs_who_are_present)`.I think a future-elixir version is going to allow
not inas a binary function though, I think…Strings are not UUID’s. Something like
"afe13f89-7819-43d9-9bb8-b4ba38a2ddac"is not a UUID but is a string. A UUID is 16 bytes of binary. The"afe13f89-7819-43d9-9bb8-b4ba38a2ddac"is a common human-readable representation but you should never ever ever pass around UUID’s in the human readable representation and should keep them as what they are, 16-byte binaries (in my humble opinion ^.^). However yes, you need to cast it with I think it isEcto.UUID.cast, not:uuid.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
ANYoperator. My original SQL query is now:This works and satisfies me.
Thanks!
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 == ^binNote that if you use an Ecto schema, you don’t need to do all this manual casting and dumping.
francois
@zambai,
{:ok, bin} = Ecto.UUID.load(room_id)is casting. I still have to do it somewhere. The schema isn’t free: the cost has to be paid somewhere.zambal
I’m not sure if I understand what your meaning. Are you worried about the performance overhead of Ecto?
francois
No, you said “you don’t need to do all this manual casting and dumping”. I only pointed out that it had to be done somewhere, either at the point of the query, or before, in the controller layer. HTTP only speaks strings. Casting will merely be done somewhere else.
benwilson512
What @zambal is saying is that if you had a schema, YOUR code wouldn’t need to worry about casting at all, Ecto would know what type it should be and will cast for you.