sergio
Trying to find all accounts with a non-zero balance using ex_money in Postgres.
zero_money = Money.new(:USD, 0)
# My schema field: field :current_balance, Money.Ecto.Composite.Type
Repo.all(
from(b in BankAccount,
where: b.user_id == ^user.id,
where: b.current_balance > ^zero_money
)
)
Error I’m getting:
** (DBConnection.EncodeError) cannot encode anonymous tuple {“USD”, Decimal.new(“0”)}. Please define a custom Postgrex extension that matches on its underlying type:
use Postgrex.BinaryExtension, type: “typeinthedb”
(postgrex 0.17.5) lib/postgrex/type_module.ex:150: Postgrex.DefaultTypes.encode_tuple/3
(postgrex 0.17.5) lib/postgrex/type_module.ex:947: Postgrex.DefaultTypes.encode_params/3
(postgrex 0.17.5) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3
I’ve reviewed the docs for both ex_money and ex_money_sql but can’t find any mentioned of where queries.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
D4no0
This question highly depends on the type you used for storing the currency.
This part of readme covers deep and wide on how to deal with money type in your database: GitHub - ex-money/money_sql: Money functions for the serialization of a money data type in Elixir · GitHub
sergio
I ran this command per the docs:
execute("CREATE TYPE public.money_with_currency AS (currency_code varchar, amount numeric);")And my schema field:
field :current_balance, Money.Ecto.Composite.TypeTried doing a search in the readme file for:
where:and couldn’t find anything. Appreciate any pointers.D4no0
Are you running the creation of this composite type?
sergio
Yes I did all that, confirmed.
I found a random Github issue with the solution:
So the type keyword is from Ecto, and it’s casting the
zero_moneyvariable to the same type as theb.current_balancecolumn?Is that how this is working?
D4no0
This is an interesting topic in itself, this is the first time I see this kind of composite map types. I would guess digging into postgres side on how they work would be beneficial for you, at the end of the day
ecto.sqlis just a DSL query builder.kip
I’m very late to this thread, apologies. I’m glad you got a resolution - and I agree that having to use the
type/2macro isn’t very ergonomic. I’ll see if following the error messages advice and building a binary extension can make this better.kip
And I should also define custom comparison operators because otherwise Postgres will happily compare two
money_with_currencytypes, even if they are of a different currency. This because Postgres, like the BEAM, has built-in term ordering.kip
I’ve sent a proposal to the Ecto mailing list about deriving the database type from an
Ecto.ParameterizedType(likeMoney.Ecto.Composite.Type) and casting to it since Ecto already knows the type involved.Unless that proposal is accepted (and I consider it a low change of success) it will be necessary to either use:
Note that
Money.zero/1is an explicit function for returning a0amount money in some currency.I will go ahead and write some custom operators for
:money_with_currencysince there is still the risk of comparing money of different currencies which is not semantically correct.sergio
Thank you @kip!
kip
It looks like there isn’t going to be a path to automatically casting the query param which means that the
type/2orfragment/2approach is going to be required for the foreseeable future.However I had neglected to mention that there are some helpers in the Money.Ecto.Query.API module. The documentation needs improving but you should find some of them help with the kind of queries you need.