Search code examples
scalascala-2.10squeryl

squeryl - saving bigDecimal to database


I'm using Scala's squeryl (version 0.9.6-RC1 with scala 2.10) and try to serialize my model to H2 database.

My model class has some string fields and some BigDecimal types (scala.BigDecimal).

I create database schema with Schema.create from my model classes, unfortunatelly squeryl always creates too small fields for BigDecimal, which leaves me with a value too long for column "MULTPZ DECIMAL(20, 16) NOT NULL": "1000000.0000000000000000 (23)";

Is this some kind of bug in squeryl? The original value is "1000000.0000" and is parsed into BigDecimal correctly.

I've tested the above situation, I've created a separate table with field defined as "DECIMAL(20, 20)" and in that case squeryl tries to insert "1000000.00000000000000000000 (27)"


Solution

  • The easiest fix is to override the defaultSizeOfBigDecimal method in your Schema. It returns a tuple containing the length and scale for the numeric column that holds a BigDecimal. You can also set those values on a per-field basis with the @Column annotation.