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

Unable to convert type: types.Decimal #803

Closed
mrz1836 opened this issue Jul 16, 2020 · 9 comments · Fixed by #1291
Closed

Unable to convert type: types.Decimal #803

mrz1836 opened this issue Jul 16, 2020 · 9 comments · Fixed by #1291

Comments

@mrz1836
Copy link

mrz1836 commented Jul 16, 2020

What version of SQLBoiler are you using (sqlboiler --version)?

v4 (4.2.0)

What is your database and version (eg. Postgresql 10)

MySQL - 8.0.19

If this happened at runtime what code produced the issue? (if not applicable leave blank)

Inserting a record, table has a field using type: decimal(5,4)

What is the output of the command above with the -d flag added to it?

unable to insert into (table_name): cannot convert type: types.Decimal

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE `test_tables` (
`decimal_field` decimal(5,4) NOT NULL DEFAULT 0.00 COMMENT 'This field has the issue'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='Test model';

Further information. What did you do, what did you expect?

I thought this issue would be fixed by now.

I saw something about hardcoding the ericlagergren decimal package, but I want to make sure I'm not missing something...

@aarondl
Copy link
Member

aarondl commented Jul 20, 2020

So what's happened is there's this proposal in Go: golang/go#30870
And this unexported interface is now in recent versions of Go: https://golang.org/src/database/sql/driver/types.go#L292
In ericlagergren/decimal's commit 50723dc this implementation was created.

What happens is the driver? (lib/pq or pkg/sql/driver) overrides the driver.Valuer implementation we have created for types.Decimal/types.NullDecimal. This means that our code is never called with newer versions of the decimal library because of the existence of this new decomposer piece.

Something appears to be wrong with this implementation, though I'm not really sure what. Though almost certainly it does not do the things that we want it to do (our decimal implementation for example forbids NaN as a value) so we'll eventually need to override this as well.

I'll file something on the decimal library and we'll see what happens.

@mrz1836
Copy link
Author

mrz1836 commented Jul 20, 2020

Much appreciated ;-)

@kardianos
Copy link

I've analyzed the root cause of the breakage: golang/go#30870 (comment)

Feedback appreciated.

@hiromaily
Copy link

hiromaily commented Feb 12, 2022

I faced same issue. Is there workaround?

SQLBoiler v4.8.6
MySQL 5.7

@zhangi
Copy link

zhangi commented Jan 6, 2023

One workaround I am using is to replace the types.Decimal in code generation:

[[types]]
  [types.match]
    type = "types.Decimal"

  [types.replace]
    type = "decimal.Decimal"

  [types.imports]
    third_party = ['"github.com/shopspring/decimal"']

@saepire
Copy link

saepire commented Jan 29, 2023

running into a similar issue where the built-in types.Decimal always fails with "NOT NULL constraint", changing no code (except for the type change) and using @zhangi solution solves it.

@benjaminW78
Copy link

@zhangi Your solution was LIFE SAVING ! i struggled 7 hours while trying to make this work with the default package and didn't think we could ask sql boiler to change default type !!! THX SO MUCH

@arthurspa
Copy link

arthurspa commented Apr 6, 2023

I'm using SqlBoiler v4.14.1 and unfortunately @zhangi 's workaround does not work for me. I get the following error when running the tests:

=== CONT  TestUpsert/Customers
    customers_test.go:837: Unable to randomize Customer struct: unsupported type: decimal.Decimal

I had to upgrade to Go 1.20.3 and use the -skip flag to skip the failing tests until this issue is solved.

@c9s
Copy link
Contributor

c9s commented Mar 17, 2024

we still have the issue here, with SQLBoiler v4.16.2

elffjs added a commit to DIMO-Network/shared that referenced this issue May 10, 2024
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

Successfully merging a pull request may close this issue.

9 participants