Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecto aggregate functions sometimes return Decimal. #219

Closed
aglassman opened this issue May 14, 2024 · 1 comment
Closed

Ecto aggregate functions sometimes return Decimal. #219

aglassman opened this issue May 14, 2024 · 1 comment

Comments

@aglassman
Copy link
Contributor

aglassman commented May 14, 2024

Problem
I modified a table's 'amount_in_cents' column to be the PostgreSQL type bigint rather than integer. This seems to have caused calls using Repo.aggregate calls to fail to load the result type into a Money struct.

schema "adjustments" do
  field :amount_in_cents, Money.Ecto.Amount.Type
  # ...
end


#

query = from(a in Adjustment, where a.wallet_id == ^wallet_id)
Repo.aggregate(query, :sum, :amount_in_cents)

This results in the following issue.

Screenshot 2024-05-14 at 1 00 57 PM

I'm not totally sure why aggregate changes it's return type from an integer to a Decimal.
This causes Money.Ecto.Amount.Type.load/1 to fail to match.

Proposed Solution
Add a new function head to the load function.

@spec load(integer()) :: {:ok, Money.t()}
    def load(int) when is_integer(int), do: {:ok, Money.new(int)}
    def load(%Decimal{} = decimal), do: Money.parse(decimal) 
@Nitrino
Copy link
Member

Nitrino commented May 16, 2024

Thanks for the description and solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants