Nathaniel Ford
Nathaniel Ford

Reputation: 21220

Type mismatch expecting NotInferedR

I'm attempting to get a basic working Slick proof working but am getting a Type Mismatch error from this line:

def * : MappedProjection[Nothing, (Option[Int], String)] = (id.?, name) <> (User.tupled, User.unapply)

Specifically, the User.tupled and User.unapply have the following errors, respectively:

Type mismatch, expected: Option[(Option[Int], String)] => NotInferedR, actual: Option[(Option[Int], String)] => User

and

Type mismatch, expected: NotInferedR => Option[(Option[Int], String)], actual: User => option[(Option[Int], String)]

I can't tell why the compiler thinks it should be NotInferedR; I can't find any documentation around this, and multiple tutorials that seem to think this is sufficient.

This is my package.scala file:

package nford

import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile

package object domain {
  trait DatabaseBacking {
    val config: DatabaseConfig[JdbcProfile]
    val db: JdbcProfile#Backend#Database = config.db
  }
}

This is my User.scala file:

package nford.domain

import nford.domain
import slick.basic.DatabaseConfig
import slick.dbio.DBIOAction
import slick.jdbc.JdbcProfile
import slick.lifted.{Index, MappedProjection}

import scala.concurrent.Future

case class User(id: Option[Int], name: Option[String])

trait UserTable {
  this: DatabaseBacking =>

  import config.profile.api._

  private class User(tag: Tag) extends Table[User](tag, "user") {
    def id: Rep[Int] = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def name: Rep[String] = column[String]("name")

    def * : MappedProjection[Nothing, (Option[Int], String)] = (id.?, name) <> (User.tupled, User.unapply)
  }

  val user = TableQuery[User]
}

How do I resolve this mismatch?

Upvotes: 0

Views: 544

Answers (1)

mklich
mklich

Reputation: 31

Late answer, but I just ran into the same issue and found this unanswered post. Unfortunately I haven't been able to figure out why it's not working. I've seen examples write it as you have done, but maybe those examples are using a Scala version older than 2.12? Anyway, I found that using mapTo works for me. So, in your case it would be:

def * : ProvenShape[User] = (id.?, name.?).mapTo[User]

(I think name also needs .? since name is an Option[String] in the User class, and the name column is defined as a String)

Upvotes: 1

Related Questions