代码之家  ›  专栏  ›  技术社区  ›  Mani Kannan

如何使用play2.6 slick3更新sql server的标识列?

  •  0
  • Mani Kannan  · 技术社区  · 7 年前

    当我尝试使用sql server运行sbt scala项目时,出现以下错误,

    [SQLServerException:无法更新标识列“ID”。]

    我正在使用Play 2.6、Scala 2.12和Slick 3

    我的更新功能是,

    def update(id: Long): Action[AnyContent] = Action.async { implicit request =>
        topicForm.bindFromRequest.fold(
          formWithErrors => Future.successful(BadRequest(html.editForm(id, formWithErrors))),
          topic => {
            val futureTopUpdate = dao.update(id, topic.copy(id = Some(id)))
            futureTopUpdate.map { result =>
              Home.flashing("success" -> "Topic %s has been updated".format(topic.code))
            }.recover {
              case ex: TimeoutException =>
                Logger.error("Problem found in topic update process")
                InternalServerError(ex.getMessage)
            }
          })
      }
    

    还有道:

    override def update(id: Long, topic: Topic): Future[Int] =
    try db.run(filterQuery(id).update(topic))
    finally db.close
    

    有什么想法吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rodo    7 年前

    您可以显示 filterQuery(id) 实现,我们的dao中类似的东西以这种方式工作得很好:

      override def update(id: Long, topic: Topic): Future[Int] = {
              db.run(filterQuery(id).update(topic.copy(id))
      }
    

    注意 :主题。 副本(id)

    而filterQuery是:

    def filterQuery(id: Int) = themes.filter(_.id === id)
    

    我们将Play 2.6、Scala 2.12、Slick 3与MYSQL配合使用。

    更新#1:

    -&燃气轮机;实体:

    case class CategoryRow(id: Int, name: String, description: String)
    

    -&燃气轮机;映射:

    trait CategoryMapping {
      self: HasDatabaseConfigProvider[JdbcProfile] =>
      import dbConfig.profile.api._
    
      private[models] class CategoryTable(tag: Tag)
        extends Table[CategoryRow](tag, "category") {
    
        def id = column[Int]("id", O.AutoInc, O.PrimaryKey)
    
        def name = column[String]("name", O.Length(TextMaxLength_250))
    
        def description = column[String]("description", Nullable)
    
        def categoryNameAgencyIndex = index("categoryName_agency_idx", (name, agencyId), unique = true)
    
        override def * = (
          id,
          name,
          description
        ) <> (CategoryRow.tupled, CategoryRow.unapply)
      }
    
      private[models] val Categories = TableQuery[CategoryTable]
      private[models] val CategoriesInsertQuery = Categories returning Categories.map(_.id)
    }
    

    -&燃气轮机;回购

     trait CategoryRepository {
        //...    
          def update(id: Int, category: Category)(implicit agencyId: Int): Future[Int]    
        //...
    
        }
    

    -&燃气轮机;重新导入:

    @Singleton
    class CategoryRepositoryImpl @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit ec: RepositoryExecutionContext)
      extends CategoryRepository with HasDatabaseConfigProvider[JdbcProfile] with CategoryMapping {
    
      import dbConfig.profile.api._
    //....
         def update(id: Int, category: CategoryRow)(implicit agencyId: Int): Future[Int] =
           db.run(filter(id).update(category.copy(id)))
    
         private def filter(id: Int) = Categories.filter(_.id === id)
    //....
    }
    

    -&燃气轮机;RepositoryExecutionContex

    class RepositoryExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")
    

    和应用。形态:

    # db connections = ((physical_core_count * 2) + effective_spindle_count)
    fixedConnectionPool = 5
    
    repository.dispatcher {
      executor = "thread-pool-executor"
      throughput = 1
      thread-pool-executor {
        fixed-pool-size = ${fixedConnectionPool}
      }
    }
    

    还有更多关于折叠的信息 Chapter 3.3 属于 Essential Slick