e94

e94

Use of binary_id in Ecto.Query

Hi,

I’m new to Elixir. So sorry if I missed something.
I cannot make this query work:

alias Uniq.UUID

mandate_uuid = "6bf40047-117d-4056-b964-e54545f901ac"
mandate_uuid = UUID.string_to_binary!(mandate_uuid)

Repo.delete_all(
  from(bp in BalancePoint,
  where:
    bp.date >= ^start_time and bp.date <= ^end_time and
    bp.mandate_uuid == type(^mandate_uuid, :binary_id)
  )
)

I ve got the following error:

value `<<107, 244, 0, 71, 17, 125, 64, 86, 185, 100, 229, 69, 69, 249, 1, 172>>` 
cannot be dumped to type :binary_id in query

Here is my schema:

  @primary_key false
  @foreign_key_type :binary_id
  schema "balances_points" do
    field(:uuid, :binary_id, primary_key: true)
    field(:date, :utc_datetime)
    field(:asset, :string)
    field(:mandate_uuid, :binary_id)
    field(:total_quantity, :decimal)

    timestamps()
  end

The following code works with Postgrex :

alias Uniq.UUID

mandate_uuid = "6bf40047-117d-4056-b964-e54545f901ac"
mandate_uuid = UUID.string_to_binary!(mandate_uuid)

query = """
    DELETE FROM balances_points
      WHERE
        date >= $2::timestamp
        and date <= $3::timestamp
        and mandate_uuid = $1;
"""
Ecto.Adapters.SQL.query(Repo, query, [mandate_uuid, start_time, end_time])

I don’t understand what’s wrong with Repo.delete_all.
Thks

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @e94 I don’t think you need to manually cast anything, you should be able to do:

mandate_uuid = "6bf40047-117d-4056-b964-e54545f901ac"

Repo.delete_all(
  from(bp in BalancePoint,
  where:
    bp.date >= ^start_time and bp.date <= ^end_time and
    bp.mandate_uuid == ^mandate_uuid
  )
)

Also Liked

codeanpeace

codeanpeace

I’d take a look at the Ecto.UUID module that comes with Ecto. In particular, it might be worth trying Ecto.UUID.cast!/1 and Ecto.UUID.dump!/1.

Off the top of my head, a possible reason the same approach is not working for the first query, but is fine for the second query is because the second query uses string interpolation.

Also, is there a particular reason why you’re using Uniq.UUID? I’d suggest seeing if Ecto.UUID can replace Uniq.UUID for your use case, especially since you’re already using Ecto to interface with Postgres. For reference, check out this article on https://pawelurbanek.com/elixir-phoenix-uuid.

joey_the_snake

joey_the_snake

Would you be able to post a small reproducible repo? What Ben said should work.

joey_the_snake

joey_the_snake

If you’d like some more info about the process Ecto uses:

  1. Your value is “dumped” into the type needed by the database driver. For Postgrex those dumped formats are listed here: Postgrex — Postgrex v0.22.2. That’s why you had a problem when you tried to convert your string before inserting it into your query for Ecto but why it worked when you did it in Postgrex directly.
  2. The query string along with the dumped values are passed to the database driver (Postgrex, for you) and the query is executed.
  3. The values produced by Postgrex in the query result are in the “dumped” format.
  4. Ecto will “load” the dumped into the schema format. Load is basically the opposite of dump.

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement