jbrb
November 12, 2019, 6:47am
1
I have a field %{total: #Money<:USD, 120.000>}
with custom money type.
I am using Ecto’s QueryApi sum like this. sum(total)
But I’m getting ERROR 42883 (undefined_function) function sum(money_with_currency) does not exist
I tried using type(sum(total), :decimal)
but also doesnt work.
If this is using ex_money you need to install the custom postgres function to sum up values of the composite type.
jbrb
November 12, 2019, 8:22am
3
Yes the app is using ex_money
, and which custom postgres function you are referring to?
# Introduction to Money SQL
![Build Status](http://sweatbox.noexpectations.com.au:8080/buildStatus/icon?job=money_sql_)
[![Hex pm](http://img.shields.io/hexpm/v/ex_money.svg?style=flat)](https://hex.pm/packages/ex_money_sql)
[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://github.com/kipcole9/money_sql/blob/master/LICENSE)
Money_SQL implements a set of functions to store and retrieve data structured as a `%Money{}` type that is composed of an ISO 4217 currency code and a currency amount. See [ex_money](https://hex.pm/packages/ex_money) for details of using `Money`. Note that `ex_money_sql` depends on `ex_money`.
## Prerequisities
* `Money_SQL` is supported on Elixir 1.6 and later only
## Serializing to a Postgres database with Ecto
`Money_SQL` provides custom Ecto data types and a custom Postgres data type to provide serialization of `Money.t` types without losing precision whilst also maintaining the integrity of the `{currency_code, amount}` relationship. To serialise and retrieve money types from a database the following steps should be followed:
1. First generate the migration to create the custom type:
```elixir
mix money.gen.postgres.money_with_currency_migration
* creating priv/repo/migrations
This file has been truncated. show original
In older versions this was part of ex_money directly, so be sure to check which version you‘re using.
2 Likes
jbrb
November 12, 2019, 9:50am
5
This must be the solution, I’ll give it a try. Thank you very much!