首页 文章

Slick:更新SQLServer / jtds上的字段

提问于
浏览
0

我正在尝试使用Slick 2.0更新字段并通过jtds驱动程序连接到SQL Server

表:

object UtilisateurSchema {

    class UtilisateurId(val value: Long) extends MappedTo[Long]

    case class Utilisateur(id: Option[UtilisateurId], nom: String, prenom: String)

    class Utilisateurs(tag: Tag) extends Table[Utilisateur](tag, "UTILISATEUR") {
        def id = column[UtilisateurId]("UTL_ID", O.PrimaryKey, O.AutoInc)
        def nom = column[String]("UTL_NOM")
        def prenom = column[String]("UTL_PRENOM")

        // Defaut projection
        def * = (id.?, nom, prenom) <>(Utilisateur.tupled, Utilisateur.unapply _)

    }
    val utilisateurs = TableQuery[Utilisateurs]
}

我的表中有一个用户,其id为 1331 .

下面的代码给了我一个例外:

val user = utilisateurs filter (  _.id === new UtilisateurId(1331L))
println(user first)
val newUser = user first() copy( nom = "Foo")
println(newUser)
user update(newUser)

例外是:

Exception in thread "main" java.sql.SQLException: Impossible de mettre à jour la colonne identité 'UTL_ID'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
    at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
    at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
    at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:506)
    at scala.slick.driver.JdbcInvokerComponent$UpdateInvoker$$anonfun$update$1.apply(JdbcInvokerComponent.scala:282)
    at scala.slick.driver.JdbcInvokerComponent$UpdateInvoker$$anonfun$update$1.apply(JdbcInvokerComponent.scala:277)
    at scala.slick.jdbc.JdbcBackend$SessionDef$class.withPreparedStatement(JdbcBackend.scala:161)
    at scala.slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:297)
    at scala.slick.driver.JdbcInvokerComponent$UpdateInvoker.update(JdbcInvokerComponent.scala:277)
    at com.sqlconnect.SqlPlayground$$anonfun$main$1.apply(SqlPlayground.scala:100)
    at com.sqlconnect.SqlPlayground$$anonfun$main$1.apply(SqlPlayground.scala:37)
    at scala.slick.backend.DatabaseComponent$DatabaseDef$class.withSession(DatabaseComponent.scala:31)
    at scala.slick.jdbc.JdbcBackend$DatabaseFactoryDef$$anon$4.withSession(JdbcBackend.scala:61)
    at com.sqlconnect.SqlPlayground$.main(SqlPlayground.scala:36)
    at com.sqlconnect.SqlPlayground.main(SqlPlayground.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Impossible de mettre à jour la colonne identité 'UTL_ID' 表示:

Impossible to update identity column 'UTL_ID'

我在家里做了一些测试,连接到MySQL驱动程序和上面的更新工作方式

另一方面,使用以下代码适用于MSSQL jtds:

val nom = for ( u <- utilisateurs if u.id === new UtilisateurId(1331L)) yield u.nom
nom.update("Foo")

谁能解释我在第一种情况下做错了什么?还是那个bug?

干杯

1 回答

  • 1

    它与您的代码或驱动程序无关; SQL Server不允许简单地编辑 IDENTITY 列;你必须使用identity insert mode .

    即使这有缺点:您一次只能在会话中的一个表上打开标识插入 .

    我没有使用SQL Server的光滑,但如果它不够聪明,不能单独保留主键,您可能必须创建自己的更新方法 .

    另请注意,光滑需要商业许可证才能与SQL Server一起使用,无论这意味着什么 . 作为closed-source package的一部分提供适当的SQL Server支持

相关问题