sqlboiler icon indicating copy to clipboard operation
sqlboiler copied to clipboard

Unable to convert type: types.Decimal

Open mrz1836 opened this issue 5 years ago • 4 comments

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...

mrz1836 avatar Jul 16 '20 17:07 mrz1836

So what's happened is there's this proposal in Go: https://github.com/golang/go/issues/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.

aarondl avatar Jul 20 '20 00:07 aarondl

Much appreciated ;-)

mrz1836 avatar Jul 20 '20 13:07 mrz1836

I've analyzed the root cause of the breakage: https://github.com/golang/go/issues/30870#issuecomment-717358078

Feedback appreciated.

kardianos avatar Oct 27 '20 16:10 kardianos

I faced same issue. Is there workaround?

SQLBoiler v4.8.6
MySQL 5.7

hiromaily avatar Feb 12 '22 11:02 hiromaily

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"']

zhangi avatar Jan 06 '23 03:01 zhangi

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.

saepire avatar Jan 29 '23 05:01 saepire

@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

benjaminW78 avatar Jan 30 '23 00:01 benjaminW78

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.

arthurspa avatar Apr 06 '23 15:04 arthurspa

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

c9s avatar Mar 17 '24 08:03 c9s