4

私はslick 2.0.2を使用しており、単純なフィルターを実行するか、whereサブステートメントを使用したいだけです。フィルター内で「and」、「or」、「not」などの論理演算を実行したいだけです。

val subjectdata = TableQuery[SubjectTable]
...
subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId.userId)).list()

エラーが発生します:

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error]       with type: (Long, String) => R
[error]   for base type: (Long, Long) => Boolean
[error]     subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()
[error]
                           ^

滑らかな 1.0.1 では、次のことができます。

val results = Query(TableClass)
.filter(r => r.isNull || r.expires > new Timestamp(DateTime.now().getMillis()))
.list

Slick2 の TableQuery で同様のことをしたいと考えています。どうやってするの?

4

1 に答える 1

10

知っておくべきことの 1 つは、Slick の操作は Scala の操作よりも型に関して厳密であるということです。両方のオペランドは、オプションでオプションでラップされた、同じ基本型を持つ必要があります。したがって、Double と Double または Option[Double] を比較しても問題ありませんが、Int と比較すると、コンパイル時にそのような警告が表示されます。エラーメッセージは、問題に向けて少しヒントを与えます

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error]       with type: (Long, String) => R
[error]   for base type: (Long, Long) => Boolean
[error]     subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()

では(Long, String) => R、引数に一致する型がなく、戻り値の型を特定できないことがわかります。したがって、 or のいずれidrs.user.get.identityIdが文字列であると仮定します。ターンはIntusingになり.toIntます。または、 を使用して db 側の値を変換することもできます.asColumnOf[String]

于 2014-05-27T00:13:30.683 に答える