Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slick insert from select

Tags:

scala

slick

I use scala 2.11 and slick 2.1.0 and have a compiled code:

   trait TSegmentClient { this: Profile =>

        import profile.simple._

        class SegmentClients(tag: Tag) extends Table[(Int, Long)](tag, "seg") {

            def segmentId = column[Int]("segment_id")
            def clientId = column[Long]("client_id")

            def * = (segmentId, clientId)
        }
    }

    segmentClients.insert(clientBehaviors.map(c => (1, c.clientId)))

it works.

But i need a case class like this:

case class SegmentClient(segmentId: Int, clientId: Long)

trait TSegmentClient { this: Profile =>

    import profile.simple._

    class SegmentClients(tag: Tag) extends Table[SegmentClient](tag, "seg") {

        def segmentId = column[Int]("segment_id")
        def clientId = column[Long]("client_id")

        def * = (segmentId, clientId) <> (SegmentClient.tupled, SegmentClient.unapply)
    }
}

segmentClients.insert(clientBehaviors.map(c => (1, c.clientId)))

But it doesn't compile.

(value: models.coper.datamining.SegmentClient)(implicit session: scala.slick.jdbc.JdbcBackend#SessionDef)Int cannot be applied to (scala.slick.lifted.Query[(scala.slick.lifted.Column[Int], scala.slick.lifted.Column[Long]),(Int, Long),Seq]) segmentClients.insert(clientBehaviors.map(c => (segmentId, c.clientId)))

What is wrong with my code?

like image 853
nosatik Avatar asked Oct 19 '22 19:10

nosatik


1 Answers

You can do this using another projection to a tuple that is not mapped to a case class.

case class SegmentClient(segmentId: Int, clientId: Long)

trait TSegmentClient { this: Profile =>

    import profile.simple._

    class SegmentClients(tag: Tag) extends Table[SegmentClient](tag, "seg") {

        def segmentId = column[Int]("segment_id")
        def clientId = column[Long]("client_id")
        def tuple = (segmentId, clientId) 
        def * = tuple <> (SegmentClient.tupled, SegmentClient.unapply)
    }
}

segmentClients.map(_.tuple).insert(clientBehaviors.map(c => (1, c.clientId)))
like image 110
cvogt Avatar answered Nov 03 '22 22:11

cvogt