Gladear

Gladear

Hello :wave:

I’m using Ecto along with PostgreSQL. I’ve been trying to add a field containing a 2-tuple to an Ecto schema, but can’t seem to make it work.

There is no built-in :tuple type in Ecto, so I tried to create an Ecto.Type, but that does not seem to be enough:

defmodule MyType do
  use Ecto.Type

  @impl Ecto.Type
  def type, do: :my_type

  @impl Ecto.Type
  def cast({one, two}) when is_binary(one) and is_binary(two), do: {:ok, {one, two}}
  def cast(_), do: :error

  @impl Ecto.Type
  def cast({one, two}) when is_binary(one) and is_binary(two), do: {:ok, {one, two}}
  def cast(_), do: :error

  @impl Ecto.Type
  def load({one, two}) do
    {:ok, {one, two}}
  end
end

I create the field in an Ecto migration using this command:

execute "CREATE TYPE my_type AS (one text, two text)"

Then, when trying to insert a struct from a schema with a field of this type, I get the following message:

(DBConnection.EncodeError) cannot encode anonymous tuple {"one", "two"}. Please define a custom Postgrex extension that matches on its underlying type:

    use Postgrex.BinaryExtension, type: "typeinthedb"

I tried to follow the instructions and create an extension, but could never make it work.

What blows my mind is that the library :ex_money_sql (cc @kip) does not seem to have to create a Postgrex extension (or at least, I could never find it). See type source. Postgrex is pretty clear on the subject: “Anonymous composite types are decoded (read) as tuples but they cannot be encoded (written) to the database” (doc). So how does it work? Why does it work? I’m a bit lost here :confused:

If you see any flaw in my reasoning, please tell me what I’m doing wrong :pray:
Thanks for your time,

Showing Posts 1 to 4

LostKobrakai

LostKobrakai

There’s no dump/1 callback in your type. Not sure if that’s related, but should be fixed non the less.

kip

kip

ex_cldr Core Team

There is a mix task that defines the money_with_currency type. Hopefully that at least clears that up :slight_smile:

kip

kip

ex_cldr Core Team

Oh, I read a bit more closely. I suspect, as @LostKobrakai says, the lack of a dump/2 callback is the issue.

Gladear

Gladear OP

@LostKobrakai oops, this was a problem when adapting my “real” schema, the second couple of “cast” clauses were actually “dump”.

Although I did find my issue when trying to make a simple reproduction: the problem was not when inserting the struct, it was when querying without the struct - something along the lines of Repo.get_by(MySchema, composite_field: {"one", "two"}). This indeed does not work, however, it can be worked around by explicitly casting to the type:

from(schema in MySchema,
  where: schema.composite_field == type(^{"one", "two"}, MyType)
)
|> Repo.one()

This works perfectly fine :ok_hand:

Thank you for your time, sorry for the bother, I may have been quite tired yesterday :see_no_evil:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
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
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews