0

我正在尝试使用unsafeSqlExtractSubField来自 Esqueleto 的方法来创建一个从日期中提取年份的方法,例如:

data ReportRow = ReportRow Text

userSignupData :: MonadIO m => Key User -> SqlPersistT m [ReportRow]
userSignupData _ = fmap toRow <$> select (from query)
  where
    query s =
      pure $ (extractYear $ s ^. UserCreatedAt)

    toRow yearVal = ReportRow (showText . unValue $ yearVal)

extractYear :: UnsafeSqlFunctionArgument a => a -> SqlExpr (Value Integer)
extractYear =
  unsafeSqlExtractSubField "year"

showText :: Show a => a -> Text
showText = T.pack . show

但我收到错误:

Could not deduce
  (UnsafeSqlFunctionArgument
    (expr0 (Value UTCTime)))

  arising from a use of ‘query’
from the context: MonadIO m
  bound by the type signature for:
    userSignupData :: forall (m :: * -> *).
                      MonadIO m =>
                      Key User -> SqlPersistT m [ReportRow]

The type variable ‘expr0’ is ambiguous

   |
20 | userSignupData _ = fmap toRow <$> select (from query)
   |                                                ^^^^^

我是否需要在这里定义一个UnsafeSqlFunctionArgumentfor的实例,UTCTime或者我是否试图将一个方形钉安装到一个圆孔中?

我不是在回答说我可以在haskell级别提取日期的答案之后,我想在查询中获取年份,以便我可以在查询中执行SQL GROUP BY

4

0 回答 0