简体   繁体   中英

How to replace id type by type parameter in this slick generic DAO trait?

How to replace concrete type Long by a type parameter ID ?

trait GenericDaoProfile[T <: HasId[Long]] {

  this: JdbcProfile =>

  import driver.api._

  trait HasIdColumn {
    def id: Rep[Long]
  }

  trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[Long, T] {
    val query: TableQuery[TB]
    override def store(t: T): Option[Long] = {
      assert(t.id.isEmpty)
      Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
    }
  }    
}

I have tried:

trait GenericDaoProfile[ID, T <: HasId[ID]] {

  this: JdbcProfile =>

  import driver.api._

  trait HasIdColumn {
    def id: Rep[ID]
  }

  trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[ID, T] {
    val query: TableQuery[TB]
    override def store(t: T): Option[ID] = {
      assert(t.id.isEmpty)
      // error for code below:
      // Error:(42, 61) No matching Shape found.
      // Slick does not know how to map the given types.
      Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
    }
  }    
}

It seems like slick doesn't know how to map arbitrary type ID but primitives. Is there any way to tell the scala compiler that ID will be a primitive type?

override def store(t: T)
(implicit shape: Shape[_ <: FlatShapeLevel, Rep[ID], ID, Rep[ID]])

You may need to provide a shape like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM