getting the following Exception (sorry its in German, but the specific MSSQL part should be clear anyhow):
Exception in thread "main" java.sql.BatchUpdateException: Ein expliziter Wert für die Identitätsspalte kann nicht in der PERSONS-Tabelle eingefügt werden, wenn IDENTITY_INSERT auf OFF festgelegt ist.
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:1160)
at scala.slick.driver.BasicInvokerComponent$InsertInvoker$$anonfun$insertAll$1$$anonfun$apply$4.apply(BasicInvokerComponent.scala:106)
at scala.slick.driver.BasicInvokerComponent$InsertInvoker$$anonfun$insertAll$1$$anonfun$apply$4.apply(BasicInvokerComponent.scala:99)
at scala.slick.session.Session$class.withPreparedStatement(Session.scala:68)
at scala.slick.session.BaseSession.withPreparedStatement(Session.scala:201)
at scala.slick.driver.BasicInvokerComponent$InsertInvoker.prepared(BasicInvokerComponent.scala:81)
at scala.slick.driver.BasicInvokerComponent$InsertInvoker$$anonfun$insertAll$1.apply(BasicInvokerComponent.scala:99)
at scala.slick.session.BaseSession.withTransaction(Session.scala:229)
at scala.slick.driver.BasicInvokerComponent$InsertInvoker.insertAll(BasicInvokerComponent.scala:95)
at org.rob.slicktests.ThirdExample$$anonfun$1.apply$mcV$sp(ThirdExample.scala:44)
at org.rob.slicktests.ThirdExample$$anonfun$1.apply(ThirdExample.scala:38)
at org.rob.slicktests.ThirdExample$$anonfun$1.apply(ThirdExample.scala:38)
at scala.slick.session.BaseSession.withTransaction(Session.scala:236)
at org.rob.slicktests.ThirdExample$delayedInit$body.apply(ThirdExample.scala:38)
import scala.slick.driver.SQLServerDriver.simple._
object ThirdExample extends App {
case class Car(id: Option[Int], marke: String, kennzeichen: String)
case class Person(id: Option[Int], name: String, age: Int, carId: Option[Int])
object Cars extends Table[Car]("CARS") {
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) // This is the primary key column
def marke = column[String]("MARKE")
def kennzeichen = column[String]("KENNZEICHEN")
// Every table needs a * projection with the same type as the table's type parameter
def * = id.? ~ marke ~ kennzeichen <> (Car, Car.unapply _)
}
// Definition of the SUPPLIERS table
object Persons extends Table[Person]("PERSONS") {
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) // This is the primary key column
def name = column[String]("NAME")
def age = column[Int]("AGE")
def carId = column[Option[Int]]("CAR_ID")
// Every table needs a * projection with the same type as the table's type parameter
def * = id.? ~ name ~ age ~ carId <> (Person, Person.unapply _)
def car = foreignKey("PERS_FK", carId, Cars)(_.id)
}
// implicit val session = Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver").createSession()
implicit val session = Database.forURL("jdbc:sqlserver://localhost:1433;DatabaseName=playground",
user = "playground",
password = "playground",
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver").createSession()
val ddl = Cars.ddl ++ Persons.ddl
session.withTransaction {
ddl.create
val p1 = Person(None, "Rob", 44, None)
val p2 = Person(None, "Sepp", 33, None)
Persons.insertAll(p1, p2)
println((for(p <- Persons) yield p).list.mkString(", "))
}
}
Any ideas how to set this properly up ? Thanks.
Cheers, Rob.