Insert slice

I am using scala 2.11 and slick 2.1.0 and have the 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's wrong with my code?

+3


source to share


2 answers


You can do this by using a different tuple projection that does not map to the 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)))

      

+2


source


Method insert

for segmentClients

the second example expects instance SegmentClient

, since segmentClients

a mapping table . This is what the compiler error message basically says. I don't know if there is a more idiomatic approach as I don't know Slick very well, but as a workaround, you can also use:



val behaviours = clientBehaviours.list.map(c => SegmentClient(1, c.clientId))
segmentClients.insertAll(behaviours)

      

0


source







All Articles