Yura Taras
Yura Taras

Reputation: 1293

Multiple representaitons for the same table in slick

I have some table with multiple columns mapped in Slick to some case class. I would like to use some subset of available columns as a query of another type. Example:

case class NodeRow(id: Long, scheduled: Int, current: Option[Int])
case class OtherModel(id: Long, current: Option[Int])
class Node(_tableTag: Tag) extends Table[NodeRow](_tableTag, Some("masterdata"), "node") {
def * = (id, scheduledFirmwareVersion, currentFirmwareVersion) <>(NodeRow.apply, NodeRow.unapply)

val id: Rep[Long] = column[Long]("id", O.AutoInc, O.PrimaryKey)
val scheduledFirmwareVersion: Rep[Int] = column[Int]("scheduled_firmware_version", O.Default(1))
// Note - in this example number of columns is reduced  
val currentFirmwareVersion: Rep[Option[Int]] = column[Option[Int]]("current_firmware_version", O.Default(None)
}
lazy val Node = new TableQuery(tag => new Node(tag))

I can use 'Node' to select instances of NodeRow. But what if I want to select only two columns to construct 'OtherModel' class? I understand I can use 'map' to select only required columns and then map on the result to convert from tuples to case classes, but I would like to know if there's some native support for such use case (including updates). Thanks.

Upvotes: 0

Views: 193

Answers (1)

pedrorijo91
pedrorijo91

Reputation: 7845

I think you can define another Slick Table upon the same db table, mapping only desired columns

Upvotes: 1

Related Questions