How do I define when the constructor arguments and table column names do not match in Skinny-ORM?

Asked 2 years ago, Updated 2 years ago, 168 views

Assume you have the following tables and classes:

sql

create table `sample`.`member`(id serial, name varchar(64));

class

 case class member (id: Long, hoge: String)

I would like to map the name column of the Member table to the member class hoge. Is there a way?

The implementations we tried are as follows:

package domain.models

import scalejdbc.WrappedResultSet
import scalejdbc.ResultName
import skinny.orm.SkinnyMapper


case class member (id: Long, hoge: String)

object Member extensions SkinnyMapper [Member] {
  override lazy val defaultAlias=createAlias("m")
  override default (rs: WrappedResultSet, n:ResultName [Member]): Member = {
    US>new Member(s)
      id=rs.get(n.id),
      hoge=rs.get (n.name)
    )
  }
}

We implemented this and did the following:

Member.findById(1)

Then,

domain.models.Member #name not found.Expected fields are #id, #hoge.

You get the error

scala playframework scalikejdbc

2022-09-30 21:13

1 Answers

If the DB column name and the model property name are different, you can override this variable for conversion.

override val nameConverters=Map("hoge"->"name")

Map ("Model Column Name" - > "DB Column Name")

package domain.models

import scalejdbc.WrappedResultSet
import scalejdbc.ResultName
import skinny.orm.SkinnyMapper


case class member (id: Long, hoge: Option [String])

object Member extensions SkinnyMapper [Member] {
  override lazy val defaultAlias=createAlias("m")
  override val nameConverters = Map("hoge" - > "name")
  override default (rs: WrappedResultSet, n:ResultName [Member]): Member = {
    US>new Member(s)
      id=rs.get(n.id),
      hoge=Some(rs.get(n.hoge))
    )
  }
}


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.