问题
I got the following error when using UUID as primary key with Slick with H2 in-memory database. I've tried to debug this for days, but no luck:
Data conversion error converting "00000000-0000-0000-0000-000000000001"
Here's the code that can reproduce this issue:
import java.util.UUID
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object Main {
class Tasks(tag: Tag) extends Table[(String, Option[UUID])](tag, "tasks") {
def name: Rep[String] = column[String]("name")
def id = column[UUID]("id", O.SqlType("UUID"), O.PrimaryKey, O.AutoInc)
def * = (name, id ?)
}
def main(args: Array[String]) {
val db = Database.forURL("jdbc:h2:mem:test;MODE=PostgreSQL", driver = "org.h2.Driver")
try {
val tasks = TableQuery[Tasks]
println("Create table: " + tasks.schema.create.statements.mkString("|"))
val setupAction: DBIO[Unit] = DBIO.seq(
tasks.schema.create,
tasks.+=("foo", None)
)
Await.result(db.run(setupAction), Duration.Inf)
} finally db.close
}
}
Can anyone provide some help here?
--- Updates ---
The table creation query is like:
create table "tasks" ("name" VARCHAR NOT NULL,"id" UUID GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY)
The full stack trace is like:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Data conversion error converting "00000000-0000-0000-0000-000000000001"; SQL statement:
insert into "tasks" ("name") values (?) [22018-187]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.value.Value.convertTo(Value.java:902)
at org.h2.value.Value.getLong(Value.java:443)
at org.h2.table.Column.updateSequenceIfRequired(Column.java:339)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:331)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:737)
at org.h2.command.dml.Insert.insertRows(Insert.java:151)
at org.h2.command.dml.Insert.update(Insert.java:114)
at org.h2.command.CommandContainer.update(CommandContainer.java:78)
at org.h2.command.Command.executeUpdate(Command.java:254)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:157)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:143)
at slick.driver.JdbcActionComponent$InsertActionComposerImpl$SingleInsertAction$$anonfun$run$7.apply(JdbcActionComponent.scala:507)
at slick.driver.JdbcActionComponent$InsertActionComposerImpl$SingleInsertAction$$anonfun$run$7.apply(JdbcActionComponent.scala:504)
at slick.jdbc.JdbcBackend$SessionDef$class.withPreparedStatement(JdbcBackend.scala:347)
at slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:407)
at slick.driver.JdbcActionComponent$InsertActionComposerImpl.preparedInsert(JdbcActionComponent.scala:498)
at slick.driver.JdbcActionComponent$InsertActionComposerImpl$SingleInsertAction.run(JdbcActionComponent.scala:504)
at slick.driver.JdbcActionComponent$SimpleJdbcDriverAction.run(JdbcActionComponent.scala:32)
at slick.driver.JdbcActionComponent$SimpleJdbcDriverAction.run(JdbcActionComponent.scala:29)
at slick.dbio.DBIOAction$$anon$4$$anonfun$run$3.apply(DBIOAction.scala:214)
at slick.dbio.DBIOAction$$anon$4$$anonfun$run$3.apply(DBIOAction.scala:214)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at slick.dbio.DBIOAction$$anon$4.run(DBIOAction.scala:214)
at slick.dbio.DBIOAction$$anon$4.run(DBIOAction.scala:212)
at slick.backend.DatabaseComponent$DatabaseDef$$anon$2.liftedTree1$1(DatabaseComponent.scala:237)
at slick.backend.DatabaseComponent$DatabaseDef$$anon$2.run(DatabaseComponent.scala:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NumberFormatException: For input string: "00000000-0000-0000-0000-000000000001"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at org.h2.value.Value.convertTo(Value.java:854)
... 31 more
回答1:
You have a strange "create table" statement with a sequence of type UUID. No database supports that. You would need to use for example:
create table tasks (name VARCHAR NOT NULL,
id UUID DEFAULT RANDOM_UUID() NOT NULL PRIMARY KEY);
insert into tasks(name) values('Hello');
select * from tasks;
But randomly generated primary keys are slow, I suggest to go back to regular sequences.
P.S. the same question was asked here: https://github.com/h2database/h2database/issues/303
来源:https://stackoverflow.com/questions/37693849/h2-in-memory-database-error-data-conversion-error-converting-when-using-uuid-a