Closed bigardone closed 11 months ago
Sorry for the inconvenience @bigardone. I believe I understand the cause. The key part of the error message is this:
Returned type character varying does not match expected type character(3) in column 1.
Which suggests that at some time you created the migration for the money_with_currency
type. Then later upgraded to a newer version of ex_money_sql
. And then you created the migration for the aggregate functions. Does that sound likely?
A few releases ago I realised that defining the money_with_currency
type as a char(3)
in Postgres was a bad idea. So I change it, and all the other sql functions, to use varchar
.
Unfortunately that means in some cases you're going to potentially have the mismatch you are seeing. The data type is char(3)
but the aggregate function is returning varchar
and Postgres won't consider them the same.
The easiest but least satisfying solution is to hand edit the aggregate function migration files to change all references to varchar
with char(3)
- thereby matching the type in the money_with_currency
migration. Then rollback the aggregate functions migration and then roll forward again.
Alternatively, if you're still in development:
money_with_currency
type and the aggregate functions.If any of that makes no sense at all, please do show me the money_sql
generated migration files so I can investigate further.
👋🏼 Hi @kipcole9, thank you very much for the response. What you suggest makes total sense. Now that we know what is happening, I'll try to find the best fix we can apply since we are already in production. I'll keep you updated and share the fix here as soon as I have it 🙌🏼
Im so sorry about this. I just haven't found a better way to automate at least notifying of the type mismatch at migration time.
I would recommend you hand-edit the migration that implements the aggregate functions. Then rollback that migration, if you can, and roll forward with the varchar
to char(3)
. The only downside is that you won't be able to use digital token currencies (crypto currency) because their codes are longer than 3 chars.
Hopefully that is workable for you?
It worked like a charm! Thank you so much for all your great libraries and for always being so friendly helping us solving issues :raised_hands:
Thank you so much for all your great libraries and for always being so friendly helping us solving issues 🙌
Thanks for the kind words, you are very welcome.
Since this error is an embarrassment (to me) I'm diving into ways to auto-detect the data type of the money_with_currency
type and create the correct version of the aggregate functions. I'm aiming to get that done by end of the weekend.
I've published ex_money_sql version 1.10.0 with the following changelog entry:
money.gen.postgres.sum_function
, money.gen.postgres.plus_operator
and money.gen.postgres.min_max_functions
) need to be aware of the type of the money_with_currency
"amount" element in a Postgres database. In releases of ex_money_sql
up to 1.7.1 the type was char(3)
. In later releases is changed to the more canonical varchar
. In turn, the database functions need to know the type for the internal accumulator. It is possible, as illustrated in issue #36, to have generated the money_with_currency
type as char(3)
and then move to a later release of ex_money_sql
. In which case the money database function migrations would fail because they were built with varchar
accumulators. This release will detect the underlying type of the money_with_currency
"amount" element and adjust the migration accordingly. Thanks to @bigardone for the report and motivation to get this done. Closes #36.-
. Thanks to @zachdaniel for the PR.Thanks again from he collaboration and patience. Its good to get this done.
😍 thank you again for the great work 🙌🏼
👋🏼 Hi there! I'm following the steps in the docs for aggregating money values, but after following the steps, I get the following error:
Here's the migration for the schema part:
Here there query I'm using:
Which creates the following query:
Other than running the migration to create the specific
sum
functions formoney_with_currency
, is there anything else I might be missing? By the way, I'm using PostgreSQL 15.Thanks in advance :raised_hands: