playframework scala slick insert query without autoincrement

时间:2017-09-08 07:33:00

标签: scala playframework playframework-2.0 slick-3.0

早上好,

我已获得以下代码段:

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]
}

它应该将一些数据插入数据库表而不生成autoincerement id。

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!

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