使用uuid_generate_v4()

时间:2015-08-18 07:34:43

标签: postgresql scala jdbc sbt slick

当我在现有的postgres 9.3数据库上使用光滑的代码生成器时,我遇到了一个错误,该数据库使用uuid_generate_v4()来自动生成表的ID。我刚刚开始使用光滑的,所以我不确定它是否是我的代码,我的架构,光滑本身,或者是否支持这种情况的问题。

作为一个例子,我有用这样的语句创建的列:

id uuid NOT NULL DEFAULT uuid_generate_v4()

当我使用sbt run运行光滑的代码生成器时,我收到下面列出的错误。此错误使代码生成器失败,因此不会生成代码。

[error] (run-main-0) java.lang.IllegalArgumentException: Invalid UUID string: uuid_generate_v4()
java.lang.IllegalArgumentException: Invalid UUID string: uuid_generate_v4()
    at java.util.UUID.fromString(UUID.java:194)
    at slick.driver.PostgresDriver$ModelBuilder$$anon$2$$anonfun$default$1.applyOrElse(PostgresDriver.scala:75)
    at slick.driver.PostgresDriver$ModelBuilder$$anon$2$$anonfun$default$1.applyOrElse(PostgresDriver.scala:65)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:223)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:219)
    at scala.Option.collect(Option.scala:282)
    at slick.driver.PostgresDriver$ModelBuilder$$anon$2.default(PostgresDriver.scala:65)
    at slick.jdbc.JdbcModelBuilder$ColumnBuilder$$anonfun$defaultColumnOption$3.apply(JdbcModelBuilder.scala:254)
    at slick.jdbc.JdbcModelBuilder$ColumnBuilder$$anonfun$defaultColumnOption$3.apply(JdbcModelBuilder.scala:254)
    at scala.Option.getOrElse(Option.scala:121)
    at slick.jdbc.JdbcModelBuilder$ColumnBuilder.defaultColumnOption(JdbcModelBuilder.scala:253)
    at slick.jdbc.JdbcModelBuilder$ColumnBuilder.convenientDefault(JdbcModelBuilder.scala:263)
    at slick.jdbc.JdbcModelBuilder$ColumnBuilder.model(JdbcModelBuilder.scala:281)
    at slick.jdbc.JdbcModelBuilder$TableBuilder$$anonfun$columns$1.apply(JdbcModelBuilder.scala:162)
    at slick.jdbc.JdbcModelBuilder$TableBuilder$$anonfun$columns$1.apply(JdbcModelBuilder.scala:162)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.Iterator$class.foreach(Iterator.scala:742)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1194)
    at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at slick.jdbc.JdbcModelBuilder$TableBuilder.columns$lzycompute(JdbcModelBuilder.scala:162)
    at slick.jdbc.JdbcModelBuilder$TableBuilder.columns(JdbcModelBuilder.scala:162)
    at slick.jdbc.JdbcModelBuilder$TableBuilder.columnsByName$lzycompute(JdbcModelBuilder.scala:164)
    at slick.jdbc.JdbcModelBuilder$TableBuilder.columnsByName(JdbcModelBuilder.scala:164)
    at slick.jdbc.JdbcModelBuilder$ForeignKeyBuilder.buildModel(JdbcModelBuilder.scala:314)
    at slick.jdbc.JdbcModelBuilder$TableBuilder$$anonfun$buildForeignKeys$1.apply(JdbcModelBuilder.scala:169)
    at slick.jdbc.JdbcModelBuilder$TableBuilder$$anonfun$buildForeignKeys$1.apply(JdbcModelBuilder.scala:169)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at slick.jdbc.JdbcModelBuilder$TableBuilder.buildForeignKeys(JdbcModelBuilder.scala:169)
    at slick.jdbc.JdbcModelBuilder$TableBuilder.buildModel(JdbcModelBuilder.scala:160)
    at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3$$anonfun$apply$17.apply(JdbcModelBuilder.scala:94)
    at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3$$anonfun$apply$17.apply(JdbcModelBuilder.scala:94)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.immutable.List.map(List.scala:285)
    at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3.apply(JdbcModelBuilder.scala:94)
    at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3.apply(JdbcModelBuilder.scala:91)
    at slick.dbio.DBIOAction$$anonfun$map$1.apply(DBIOAction.scala:43)
    at slick.dbio.DBIOAction$$anonfun$map$1.apply(DBIOAction.scala:43)
    at slick.backend.DatabaseComponent$DatabaseDef$$anonfun$runInContext$1.apply(DatabaseComponent.scala:146)
    at slick.backend.DatabaseComponent$DatabaseDef$$anonfun$runInContext$1.apply(DatabaseComponent.scala:146)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1253)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1346)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
java.lang.RuntimeException: Nonzero exit code: 1
    at scala.sys.package$.error(package.scala:27)
    at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1943)
    at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1943)
    at scala.Option.foreach(Option.scala:236)
    at sbt.BuildCommon$class.toError(Defaults.scala:1943)
    at sbt.package$.toError(package.scala:4)
    at $4f4c3fbf06757612bb06$$anonfun$slickCodeGenTask$1.apply(build.sbt:27)
    at $4f4c3fbf06757612bb06$$anonfun$slickCodeGenTask$1.apply(build.sbt:19)
    at scala.Function4$$anonfun$tupled$1.apply(Function4.scala:35)
    at scala.Function4$$anonfun$tupled$1.apply(Function4.scala:34)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
    at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
    at sbt.std.Transform$$anon$4.work(System.scala:63)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
    at sbt.Execute.work(Execute.scala:235)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226)
    at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    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)
[error] (compile:managedSources) Nonzero exit code: 1

以下是我的sbt项目的详细信息......

项目/ build.properties

sbt.version = 0.13.8

项目/ plugins.sbt

logLevel := Level.Warn

build.sbt

name := "slick_gen_schema_test"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++= List(
  "org.postgresql" % "postgresql" % "9.3-1103-jdbc4",
  "com.typesafe.slick" % "slick_2.11" % "3.0.2",
  "com.typesafe.slick" % "slick-codegen_2.11" % "3.0.2"
)

slick <<= slickCodeGenTask

sourceGenerators in Compile <+= slickCodeGenTask

lazy val slick = TaskKey[Seq[File]]("gen-tables")
lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
  val outputDir = (dir / "main/slick").getPath
  val username = "postgres"
  val password = "postgres"
  val url = "jdbc:postgresql://10.0.11.13:5432/example"
  val jdbcDriver = "org.postgresql.Driver"
  val slickDriver = "slick.driver.PostgresDriver"
  val pkg = "dao"
  toError(r.run("slick.codegen.SourceCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg, username, password), s.log))
  val fname = outputDir + "/dao/Tables.scala"
  Seq(file(fname))
}

在这种情况下使用光滑代码生成器的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

这是一个Slick错误,已在Slick master(https://github.com/slick/slick/pull/1472)中修复。

在Slick 3.2发布之前,您可以通过将架构更改为没有功能默认值来解决此问题。