我收到以下错误,但我不明白错误。
could not find implicit value for parameter tm: scala.slick.ast.TypedType[I]
[error] def id = column[I]("id",O.PrimaryKey)
[error] ^
此错误指的是FkTable。我列出了代码,以及显示我意图的评论。我的计划是为表格打造一个很好的基础,然后我可以将我的CRUD服务关闭。
//ROWS
trait Row[I<:AnyVal,M[_]]{
val id:M[I]
}
//TABLES
/**
* @tparam I This is the type of the ID column in the table
* @tparam M The row may contain a wrapper (such as Option) which communicates extra information to Slick for the row
* data. M can be Id, which means that M[I] == Id[I] == I which means the Row item can contain a simple type.
* @tparam R The class that represents a row of data. As explained above, it is described by its __id__ which is of
* type M[I].
*/
abstract class BasicTable[I <:AnyVal,M[_], R <:Row[I,M]](tag: Tag, name: String) extends Table[R](tag, name) {
def id: Column[I]
}
/**
* @note The BasicTable takes Long,Option because longs can be incremented, and in slick this is communicated by an
* Option value of None. The None is read and tells Slick to generate the next id value. This implies that the
* Row (R) must have a value __id__ that is an Option[Long], while the id column must remain Long.
*/
abstract class IncrTable[R <: Row[Long,Option]](tag: Tag, name: String) extends BasicTable[Long,Option,R](tag, name){
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
}
/**
*
* @param refTable
* @tparam I This is the type of the ID column in the table
* @tparam R The class that represents a row of data. As explained above, it is described by its __id__ which is of
* type M[I].
* @tparam M2 The Wrapper for the referenced table's id value
* @tparam R2 The class that represents the referenced table's row of data
*/
abstract class FkTable[I<: AnyVal,R<:Row[I,Id],M2[_], R2 <:Row[I,M2]](tag: Tag, name: String,refTable:TableQuery[BasicTable[I,M2,R2]]) extends BasicTable[I,Id,R](tag, name){
def id = column[I]("id", O.PrimaryKey)
//Foreign Key
def fkPkconstraint = foreignKey("PK_FK", id, refTable)(_.id)
}
答案 0 :(得分:4)
Slick的def column[T]
在其签名中请求隐式TypedType[T]
。光滑的TypedType
用于Int
,Double
,Date
等,但它找不到任何一种类型I
。您可以在构造函数中向FkTable
请求一个。只需将第二个参数列表添加到FkTable (implicit tt: TypedType[I])
:
abstract class FkTable[...](...)(implicit tt: TypedType[I]) extends ...
这会将隐式查找移动到使用FkTable
的位置,I
可能设置为更具体的类型。然后,TypedType
值会隐式传播到column[I]
的调用。