我一直在玩underscoreio/slickless试图使用Records而不是HLists。我希望它可以开箱即用,因为无形的Record似乎是简单的HLists(带标签),但是没有光滑的插件无法从Record中创建ProvenShape
,我想知道为什么。
我打算使用slick / codegen模块生成这样的表类:
class Users( tag: Tag ) extends Table[Record.`'id -> Rep[Long], 'name -> Rep[String], 'age -> Rep[Int]`.T]( tag, "users" ) {
def id = column[Long]( "id", O.PrimaryKey, O.AutoInc )
def name = column[String]( "name" )
def age = column[Int]( "age" )
val row: Record.`'id -> Rep[Long], 'name -> Rep[String], 'age -> Rep[Int]`.T = {
( 'id ->> id ) :: ( 'name ->> name ) :: ( 'age ->> age ) :: HNil
}
def * : ProvenShape[Record.`'id -> Rep[Long], 'name -> Rep[String], 'age -> Rep[Int]`.T] = ??? // row
}
object Users extends TableQuery( new Users( _ ) )
但不幸的是,* = row
分配失败了:
[error] Users.scala:19: type mismatch;
[error] found : shapeless.::[slick.lifted.Rep[Long] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("id")],slick.lifted.Rep[Long]],shapeless.::[slick.lifted.Rep[String] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("name")],slick.lifted.Rep[String]],shapeless.::[slick.lifted.Rep[String] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("email")],slick.lifted.Rep[String]],shapeless.HNil]]]
[error] required: slick.lifted.ProvenShape[shapeless.::[slick.lifted.Rep[Long] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("id")],slick.lifted.Rep[Long]],shapeless.::[slick.lifted.Rep[String] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("name")],slick.lifted.Rep[String]],shapeless.::[slick.lifted.Rep[String] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("email")],slick.lifted.Rep[String]],shapeless.HNil]]]]
[error] def * : ProvenShape[Record.`'id -> Rep[Long], 'name -> Rep[String], 'email -> Rep[String]`.T] = row
[error] ^
[error] one error found
[error] (data/compile:compileIncremental) Compilation failed
如果它有效,我将能够以简单和类型安全的方式剥离列,这将是一个巨大的收获:
case class User( name: String, age: Int )
add( user: User ) = {
Users.map( _.row - 'id ) += Generic[User].to( user )
}