I have two models (say, Supplier and Coffee) and Coffee model has foreign key reference to Supplier model. During ddl, I want this relationship to exist in table creation. But I also want to be able to refer the Supplier object through Coffee object like coffeeObj.supplier.name
. Below is my dummy code. I am using MappedTable, foreignKey and TypeMapper.
import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession
object SlickTest {
// Supplier
case class Supplier(id: Option[Int], name: String)
object Suppliers extends Table[Supplier]("supplier") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = id.? ~ name <> (Supplier, Supplier.unapply _)
def findOneById(id: Int): Supplier =
this.map { e => e }.where(r => r.id === id).firstOption.get
}
// Implicit Conversion from Supplier to Int
implicit val supTypeMapper = MappedTypeMapper.base[Supplier, Int](
{ s => s.id.get }, { id => Suppliers.findOneById(id) })
// Coffee
case class Coffee(name: Option[String], sup: Supplier, price: Double)
object Coffees extends Table[Coffee]("coffee") {
def name = column[String]("cof_name", O.PrimaryKey)
def sup = column[Supplier]("supplier")
def price = column[Double]("price")
def * = name.? ~ sup ~ price <> (Coffee, Coffee.unapply _)
def supplier = foreignKey("SUP_FK", sup, Suppliers)(_.id)
}
}
The code is failing at the last line for the definition of supplier
. Could anyone shed any light?
coffeeObj.supplier.name
? – Mufi