Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Slick / ScalaQuery BigDecimal creates decimal(10,0) how to allow decimals?

How do I tell Slick to create a Decimal SQL type which allows for decimals?
It seems that by default Slick doesn't allows for decimals which I noticed with the below code. It creates a column with the data type decimal(10,0) in MySQL.

I have the following model in my Scala code:

import scala.slick.driver.MySQLDriver.simple._

case class TimeType(id: Option[Int], name: String,
  employeeMultiplier: BigDecimal, employerMultiplier: BigDecimal)

object TimeTypes extends Table[TimeType]("timeType") {
  def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  def employeeMultiplier = column[BigDecimal]("employeeMultiplier")
  def employerMultiplier = column[BigDecimal]("employerMultiplier")
  def * = id.? ~ name ~ employeeMultiplier ~ employerMultiplier <> (TimeType, TimeType.unapply _)
}

During startup I create this table using Slick unless it already exists with this code

...
lazy val database = Database.forDataSource(DB.getDataSource())

database.withSession {
  if (MTable.getTables("timeType").list.isEmpty) {
    TimeTypes.ddl.create
    ...
like image 760
Farmor Avatar asked Jan 15 '13 12:01

Farmor


1 Answers

It's not ideal, but you could try this:

def employeeMultiplier = 
  column[BigDecimal]("employeeMultiplier", O.DBType("decimal(10, 4)"))

MySQL accepts just DECIMAL, in which case it creates the field with it's defaults that happen to be DECIMAL(10, 0). I'm not sure if there's any other way to tell Slick/ScalaQuery that you want certain options for your fields.

like image 102
Faiz Avatar answered Nov 21 '22 00:11

Faiz