gpt4 book ai didi

scala - 从选择中插入

转载 作者:行者123 更新时间:2023-12-03 16:13:44 25 4
gpt4 key购买 nike

我使用 scala 2.11 和 slick 2.1.0 并编译代码:

   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)))

有用。

但我需要一个这样的案例类:
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)))

但它不编译。

(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)))



我的代码有什么问题?

最佳答案

您可以使用对未映射到案例类的元组的另一个投影来执行此操作。

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)))

关于scala - 从选择中插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27902774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com