Md. Shougat Hossain
Md. Shougat Hossain

Reputation: 521

How to join two tables and map the result to a case class in slick

I am working in a project with scala play 2 framework where i am using slick as FRM and postgres database.

In my project customer is an entity. So i create a customer table and customer case class and object also. Another entity is account. So i create a account table and account case class and object also. The code is given bellow

case class Customer(id: Option[Int],
            status: String,
            balance: Double,
            payable: Double,
            created: Option[Instant],
            updated: Option[Instant]) extends GenericEntity {
def this(status: String,
   balance: Double,
   payable: Double) = this(None, status, balance, payable, None, None)
}
class CustomerTable(tag: Tag) extends GenericTable[Customer](tag, "customer"){
   override def id = column[Option[Int]]("id")
   def status = column[String]("status")
   def balance = column[Double]("balance")
   def payable = column[Double]("payable")
   def account = foreignKey("fk_customer_account", id, Accounts.table)(_.id,    onUpdate = ForeignKeyAction.Restrict, onDelete = ForeignKeyAction.Cascade)

def * = (id, status, balance, payable, created, updated) <> ((Customer.apply _).tupled, Customer.unapply)
}
object Customers extends GenericService[Customer, CustomerTable] {
   override val table = TableQuery[CustomerTable]
   val accountTable = TableQuery[AccountTable]
   override def copyEntityFields(entity: Customer, id: Option[Int],
   created: Option[Instant], updated: Option[Instant]): Customer = {
      entity.copy(id = id, created = created, updated = updated)
   }
}

Now I Want to join Customer Table and Account Table and map the result to a case class named CustomerWithAccount I have tried the following code

case class CustomerDetail(id: Option[Int],
                      status: String,
                      name: String)
object Customers extends GenericService[Customer, CustomerTable] {
   override val table = TableQuery[CustomerTable]
   val accountTable = TableQuery[AccountTable]
   def getAllCustomersWithAccount = db.run(table.join(accountTable).on(_.id === _.id).map { row =>
   //for (row1 <- row) {
     for {
       id <- row._1.id
       status <- row._1.status.toString()
       name <- row._2.name.toString()
     } yield CustomerDetail(id = id, status = status, name = name)
   //}
   }.result)
   override def copyEntityFields(entity: Customer, id: Option[Int], created:Option[Instant], updated: Option[Instant]): Customer = {
    entity.copy(id = id, created = created, updated = updated)
  }
}

But this did not work. Please help me.

Upvotes: 3

Views: 3891

Answers (2)

Andrew Norman
Andrew Norman

Reputation: 911

you can do this with 3 case classes, 1 per table and then 1 for the joined result.

db.run((customerTable.join(accountTable).on(_.id === _.id)
  .map{
     case (c,a) => CustomerWithAccount(status = c.status, created = 
                    c.created, account=a, ...)
   }

Upvotes: 0

Yuan Xing
Yuan Xing

Reputation: 96

You can try this query

db.run((table.join(accountTable).on(_.id === _.id)
  .map{
    case (t,a) => ((t.id, t.status, a.name) <> (CustomerDetail.tupled, CustomerDetail.unapply _))
  }).result)

Upvotes: 7

Related Questions