【问题标题】:Type mismatch when trying to use generic JdbcDriver usage in Slick / Scala尝试在 Slick / Scala 中使用通用 JdbcDriver 用法时类型不匹配
【发布时间】:2016-07-31 01:45:03
【问题描述】:

我一直在尝试使用通用 JdbcDriver 来使用 Slick,但遇到了 Scala 类型不匹配错误。如果我在不使用 Slick 的情况下尝试此操作并滚动我自己的超级简单 Table & TableQuery 模拟,则不会出现问题,因此请相信该错误与 Slick 对 Scala 的一些更复杂的使用有关。

以下示例使用 Slick 3.1.1。我也尝试过使用 Slick 3.2.0-M1 进行等效操作(在必要时将驱动程序更改为配置文件),但出现了相同的错误。

StackOverflow 上似乎有许多“类型不匹配”问题/答案,但我无法解释它们是否与此问题相关,因此感谢您对改进 Scala/Slick 使用的任何帮助。

以下代码产生:

*Error:(37, 25) type mismatch;
 found   : slick.lifted.TableQuery[dao.PersonTable]
 required: slick.lifted.TableQuery[_ <: _2.driver.Table[_]] where val _2: test.Test.ProfileUser
    dbDriver.create(dao.persons) // Creates this error...*

package test

import slick.backend.DatabaseConfig
import slick.driver.JdbcDriver

trait GenericDriver {
  val driver: JdbcDriver
}

case class PersonRecord(id: Int, name: String)

trait Person {
  self: GenericDriver =>

  import driver.api._

  class PersonTable(tag: Tag) extends Table[PersonRecord](tag, "persons") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def name = column[String]("name")
    def * = (id, name) <> (PersonRecord.tupled, PersonRecord.unapply)
  }

  val persons = TableQuery[PersonTable]
}

class Dao(val driver: JdbcDriver)
  extends Person
    with GenericDriver

object Test {

  def main(args: Array[String]): Unit = {
    val dbConfig = DatabaseConfig.forConfig[JdbcDriver]("sampleDb")
    val dbDriver = dbConfig.driver
    val dao = new Dao(dbDriver)

    dbDriver.create(dao.persons) // Creates this error...
    //
    //    Error:(37, 25) type mismatch;
    //    found   : slick.lifted.TableQuery[dao.PersonTable]
    //    required: slick.lifted.TableQuery[_ <: _2.driver.Table[_]] where val _2: test.Test.ProfileUser
    //    dbDriver.create(dao.persons) // Creates this error...
    //
  }

  implicit class ProfileUser(val driver: JdbcDriver) extends GenericDriver {
    import driver.api._
    def create(tq: TableQuery[_ <: Table[_]]) = tq.schema.create
  }

}

供参考 - build.sbt 是:

name := "test_import"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "com.typesafe.slick" %% "slick" % "3.1.1"
)

【问题讨论】:

  • 我也尝试过使用import driver.profile.api._。然而,这会产生类似的错误消息:Error:(37, 25) type mismatch; found : dao.driver.profile.api.TableQuery[_$1] where type _$1 &lt;: dao.driver.profile.api.Table[_] (which expands to) slick.lifted.TableQuery[_$1] required: slick.lifted.TableQuery[_ &lt;: _2.driver.profile.Table[_]] where val _2: test.Test.ProfileUser dbDriver.create(dao.persons)

标签: scala jdbc slick type-mismatch


【解决方案1】:

这里的问题原来是路径依赖类型。

这个(StackOverflow)问答帮助我朝着正确的方向前进: Scala Type Mismatch issue when using import from different scope.

这篇(外部)文章有助于进一步了解 PDT: http://danielwestheide.com/blog/2013/02/13/the-neophytes-guide-to-scala-part-13-path-dependent-types.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 2017-10-31
    • 2011-10-19
    相关资源
    最近更新 更多