将 TableQuery 对象作为参数传递会导致 Slick/Scala 中出现奇怪的类型错误,但在注入时不会

Passing TableQuery object as a parameter results in strange type error in Slick/Scala but not when injected

我正在编写一个函数来将一些数据保存到 Slick postgres 数据库中。这是我的 table 持久化 ClientRow 对象:

class ClientTable(tag: Tag) extends Table[ClientRow](tag, "clients") {
  def clientId = column[Long]("client_id", O.PrimaryKey, O.AutoInc)
  ...
  override def * =
        (clientId, phoneNumber, firstName, lastName, trainerId, dateRegistered) <> (ClientRow.tupled, ClientRow.unapply _)
}

我尝试编写以下函数以通过组合注入 table 和数据库依赖项:

type PersistClient[A <: AbstractTable[_]] =
    DatabaseDef => TableQuery[A] => ClientInfo => Future[ClientId]

val persist: PersistClient[ClientTable] = db =>
      clients =>
        info => {
          val action = clients.returning(
            clients.map(_.clientId) += toRow(info)
          )
          db.run(action).map(constructId(_))
    }

这里的 ClientInfo 和 ClientId 只是 class 数据的大小写。 toRow 具有签名 ClientInfo => ClientRow,它将 Info 对象中的强 typed/highly 结构化数据转换为 SQL 的更原始数据。 constructId 从 Long 中创建一个字符串 ID 以提高日志可读性(我喜欢 id.client.<number>

toRow下我得到以下错误:

type mismatch;
 found   : messager.Client.persistence.ClientRow
 required: Long

奇怪的是,如果我编写相同的函数,同时将依赖项注入包含此函数的 class:

val persistClient: ClientInfo => Future[ClientId] = info => {
      val action = clients.returning(clients.map(_.clientId)) += toRow(info)
      db.run(action).map(constructId(_))
    }

这里的 clientsdb 与之前的类型相同,但只是作为依赖项而不是函数的输入注入到 class 中。是什么赋予了?为什么我的柯里化代码无法编译?

看起来 clients.returning 的参数是 returns 行数更改的插入。这看起来不对,而且与您使用的版本不同。