Slick 索引导致抛出 InvocationTargetException

Slick index causes InvocationTargetException to be thrown

我有以下 Slick Table 定义:

class FooRecords(tag: Tag) extends Table[Foo](tag, Foo.TableName) {
  def id = column[Int]("ID", O.PrimaryKey)
  def teamId = column[Int]("TEAM_ID")
  // ... other columns
  def * = (id, teamId, /* ... */) <> ((FooRecord.apply _).tupled, FooRecord.unapply)
  def teamIdIdx = index("team_id_idx", teamId, unique = false)
}

// ...

val fooRecords = TableQuery[FooRecords]
fooRecords.ddl.createStatements.foreach(x => info(x))

如果注释了def teamIdIdx行,就可以正常工作(但显然没有创建索引)。如果没有评论,我会得到一个 InvocationTargetException:

 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at scala.slick.lifted.AbstractTable$$anonfun$indexes.apply(AbstractTable.scala:105)
        at scala.slick.lifted.AbstractTable$$anonfun$indexes.apply(AbstractTable.scala:103)
        at scala.collection.TraversableViewLike$Mapped$$anonfun$foreach.apply(TraversableViewLike.scala:169)
        at scala.collection.TraversableViewLike$Filtered$$anonfun$foreach.apply(TraversableViewLike.scala:197)
        at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
        at scala.collection.mutable.IndexedSeqLike$$anon.foreach(IndexedSeqLike.scala:52)
        at scala.collection.TraversableViewLike$Filtered$class.foreach(TraversableViewLike.scala:196)
        at scala.collection.mutable.IndexedSeqView$$anon.foreach(IndexedSeqView.scala:80)
        at scala.collection.TraversableViewLike$Mapped$class.foreach(TraversableViewLike.scala:168)
        at scala.collection.SeqViewLike$$anon.foreach(SeqViewLike.scala:197)
        at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
        at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
        at scala.collection.ViewMkString$class.thisSeq(TraversableViewLike.scala:22)
        at scala.collection.SeqViewLike$AbstractTransformed.thisSeq(SeqViewLike.scala:37)
        at scala.collection.SeqViewLike$$anonfun$sortBy.apply(SeqViewLike.scala:266)
        at scala.collection.SeqViewLike$$anonfun$sortBy.apply(SeqViewLike.scala:266)
        at scala.collection.SeqViewLike$$anon.<init>(SeqViewLike.scala:195)
        at scala.collection.SeqViewLike$class.newForced(SeqViewLike.scala:195)
        at scala.collection.SeqViewLike$AbstractTransformed.newForced(SeqViewLike.scala:37)
        at scala.collection.SeqViewLike$class.sortBy(SeqViewLike.scala:266)
        at scala.collection.SeqViewLike$AbstractTransformed.sortBy(SeqViewLike.scala:37)
        at scala.slick.lifted.AbstractTable.indexes(AbstractTable.scala:105)
        at scala.slick.driver.JdbcStatementBuilderComponent$TableDDLBuilder.<init>(JdbcStatementBuilderComponent.scala:597)
        at scala.slick.driver.SQLiteDriver$TableDDLBuilder.<init>(SQLiteDriver.scala:192)
        at scala.slick.driver.SQLiteDriver$class.createTableDDLBuilder(SQLiteDriver.scala:133)
        at scala.slick.driver.SQLiteDriver$.createTableDDLBuilder(SQLiteDriver.scala:260)
        at scala.slick.driver.SQLiteDriver$.createTableDDLBuilder(SQLiteDriver.scala:260)
        at scala.slick.driver.JdbcProfile$class.buildTableSchemaDescription(JdbcProfile.scala:39)
        at scala.slick.driver.SQLiteDriver$.buildTableSchemaDescription(SQLiteDriver.scala:260)
        at scala.slick.driver.SQLiteDriver$.buildTableSchemaDescription(SQLiteDriver.scala:260)
        at scala.slick.profile.RelationalProfile$TableQueryExtensionMethods.ddl(RelationalProfile.scala:52)

我在 "com.typesafe.slick" %% "slick" % "2.1.0" Android 19.

这是由于 Proguard 消除了 index 方法。调整 build.sbt 为:

proguardOptions in Android ++= Seq(
  "-keep class scala.slick.lifted.**",
  // ...
}

有帮助。