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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
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
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (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.