为Slick的MappedProjection指定返回类型

时间:2013-06-18 01:50:18

标签: scala playframework-2.1 slick

我将PlayFramework 2.1.1与Slick 1.0.1和Play-Slick-Plugin 0.3.2结合使用。

定义一个强制我的模型实现“forInsert”-Mapper的抽象类失败,因为我无法指定正确的返回类型。我当前的定义导致下面的编译错误,但我无法追踪此问题并提供正确的类型。

import play.api.db.slick.Config.driver.KeysInsertInvoker

abstract class Model[M]( val table: String ) extends Table[M]( table )
{
    def id = column[Int]( "id", O.PrimaryKey, O.AutoInc )

    def forInsert: KeysInsertInvoker[M, Int]
}

object Course extends Model[Course]( "course" )
{
    ...

    def forInsert = name ~ room <> ( apply _, unapply _ ) returning id
}

[error] Course.scala:27: polymorphic expression cannot be instantiated to expected type;
[error]  found   : [RU]play.api.db.slick.Config.driver.KeysInsertInvoker[model.Course,RU]
[error]  required: play.api.db.slick.Config.driver.KeysInsertInvoker[model.Course,Int]
[error]         def forInsert = name ~ room <> ( apply _, unapply _ ) returning id
[error]                                                               ^
[error] one error found
[error] (sample/compile:compile) Compilation failed
[error] Total time: 3 s, completed 18.06.2013 03:38:24

1 个答案:

答案 0 :(得分:0)

abstract class Model[M]( val table: String ) extends Table[M]( table )
{
    def id = column[Int]( "id", O.PrimaryKey, O.AutoInc )

    def forInsert: scala.slick.driver.BasicInvokerComponent#KeysInsertInvoker[M, Int]
}

没那么难。从实现中打印getClass很容易解开谜团。我昨天晚上没想到的一个想法。