没有自动增量的playframework scala slick插入查询

playframework scala slick insert query without autoincrement

早上好,

我得到了以下代码片段:

def createResponsibleProcessTemplates(processTemplateId: Int, username: String): Future[Int] = db.run {
    println("Create Responsible TemplateId: " + processTemplateId + " UserName: " + username)
    (responsibleProcessTemplates
      returning responsibleProcessTemplates.map(_.processTemplateId)
      ) += ResponsibleProcessTemplateModel(processTemplateId, username)
  }

我的模特是:

package models

import play.api.libs.json.Json

case class ResponsibleProcessTemplateModel(
                                  processTemplateId: Int,
                                  username: String)

object ResponsibleProcessTemplateModel {
  implicit val responsibleProcessTemplateFormat = Json.format[ResponsibleProcessTemplateModel]
}

特征看起来像这样:

trait ResponsibleProcessTemplateComponent {
  self: HasDatabaseConfigProvider[JdbcProfile] =>

  import profile.api._

  class ResponsibleProcessTemplate(tag: Tag) extends Table[ResponsibleProcessTemplateModel](tag, "Responsible_ProcessTemplates") {
    def processTemplateId: Rep[Int] = column[Int]("processTemplateId")

    def username: Rep[String] = column[String]("username")

    def * : ProvenShape[ResponsibleProcessTemplateModel] = (processTemplateId, username) <> ((ResponsibleProcessTemplateModel.apply _).tupled, ResponsibleProcessTemplateModel.unapply)
  }

  val responsibleProcessTemplates: TableQuery[ResponsibleProcessTemplate] = TableQuery[ResponsibleProcessTemplate]
}

它应该将一些数据插入数据库 table 而不生成自动增量 ID。

我没有收到错误,但数据未存储在数据库中。

感谢您的帮助。

我找到了解决方案!

  def createResponsibleProcessTemplates(processTemplateId: Int, username: String): Future[Int] = {
     db.run(responsibleProcessTemplates.map(x => (x.processTemplateId, x.username)) += (processTemplateId, username))
 }