thenrio
I have an Ecto.Type Size that maps to postgres composite type size.
I can encode a Size parameter but can’t encode an array of Size.
Here some details, the postgres type
CREATE TYPE size AS (value numeric, unit varchar(255))
The Ecto.Type has a basic implementation, type/1, cast/1, dump/1 and load/1 (I omit the details).
Encode a Size parameter works (A):
> q = from(d in Display, where: d.size == type(^s43, Size) and d.id == 1440, select: d.id); Repo.to_sql(:all, q)
{"SELECT d0.\"id\" FROM \"displays\" AS d0 WHERE ((d0.\"size\" = $1::size) AND (d0.\"id\" = 1440))",
[{43, "inch"}]}
> Repo.all(q)
[1440]
But encode an array of Size does not (B):
q = from(d in Display, where: d.size in type(^[{43, "inch"}], {:array, Size}) and d.id == 1440, select: d.id); Repo.to_sql(:all, q)
{"SELECT d0.\"id\" FROM \"displays\" AS d0 WHERE (d0.\"size\" = ANY($1) AND (d0.\"id\" = 1440))",
[[{43, "inch"}]]}
Repo.all(q)
** (DBConnection.EncodeError) cannot encode anonymous tuple {43, "inch"}. Please define a custom Postgrex extension that matches on its underlying type:
use Postgrex.BinaryExtension, type: "typeinthedb"
It looks like some typing was lost there, when I use {:array, Size}, I want to mean size[].
Note that provide explicit cast of param to size[] works, at the sql level:
Repo.query!("select id from displays where size=any($1::size[]) and id=1440", [[{43, "inch"}]]).rows
[[1440]]
Is there something missing in my ecto expression (B)?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
thenrio
A containment is fragment:
dimitarvp
Problem seems to be that
postgrexdoes not know how to handle{43, "inch"}i.e. it does not map it to thesizetype – when it’s in a list / array that is.You already arrived at a good workaround IMO. But let’s list some more options:
(A)
Use
fragmentsimilar to what you already did:I have not tested this, just going by memory and docs. Should work but, you know, famous last words.
(B)
Define a custom type through
Postgrex.BinaryExtensionas the error message is guiding you to do; more or less write a small extension forpostgrex. You’ll have to implement a protocol and make sure the PostgreSQL extension is loaded on startup. None of these are a big problem but still, good to know what you are getting yourself into.I’d be tempted to just try this with
Ecto.Typebut we’re talking the wire protocol details here, not a transparent serialization of a type that’s a composite of others that are already well-handled.All of that being said, your escape hatch is IMO quite fine.