我在Slick中定义了一个下表,它通过ID连接两个实体,因此它由两个ID组成,它们一起构成主键。我不知道如何向表中添加新值。
trait CaptionNounComponent extends CaptionComponent with NounComponent {
self: HasDatabaseConfigProvider[JdbcProfile] =>
import profile.api._
class CaptionNounTable(tag: Tag) extends Table[CaptionNoun](tag, "caption_noun") {
def * : ProvenShape[CaptionNoun] = (captionId, nounId) <> ((CaptionNoun.apply _).tupled, CaptionNoun.unapply)
def nounId: Rep[Int] = column[Int]("NOUN_ID")
def captionId: Rep[Int] = column[Int]("CAPTION_ID")
def pk = primaryKey("primaryKey", (captionId, nounId))
def nounFK =
foreignKey("noun_fk", nounId, TableQuery[NounTable])(_.id)
def captionFK =
foreignKey("caption_fk", captionId, TableQuery[CaptionTable])(_.id)
}
}
通常我会尝试类似的东西:
private val keyTable = TableQuery[CaptionNounTable]
def addKeys(keys: CaptionNoun): Future[Int] = db.run {
(keyTable returning keyTable.map(_.pk)) += keys
}
但在这种情况下不起作用
我忘了进口
import profile.api._
这似乎解决了问题