使用RawSql更新时出现模糊类型错误(Ambiguous Type Error When Using RawSql Update)

编程入门 行业动态 更新时间:2024-10-25 04:22:11
使用RawSql更新时出现模糊类型错误(Ambiguous Type Error When Using RawSql Update)

我试图基于其他列更新表的列,但看起来我无法使用updateWhere函数。 所以我尝试使用rawSQl但由于模糊的类型错误而无法正常工作。

getCalculateDeltaR :: personId -> Handler Html getCalculateDeltaR personId = do goods <- runDB $ selectList [GoodPerson ==. personId] [] forM goods $ \(Entity gid good) -> do let aid = goodAsset good asset <- runDB $ get404 aid let mktValue = assetMktValue asset runDB $ rawSql "UPDATE good SET delta = (? - orig_value) \ WHERE person = ? AND asset = ?" [toPersistValue mktValue, toPersistValue personId, toPersistValue aid] defaultLayout $ do $(widgetFile "calculateDelta")

我收到以下错误,我确实尝试添加? 更新后,但没有什么区别。

Ambiguous type variable `a0' in the constraint: (RawSql a0) arising from a use of `rawSql' Probable fix: add a type signature that fixes these type variable(s) In the second argument of `($)', namely `rawSql ... ...

我可以传递给rawSQL的参数定义得很好,所以不确定是什么导致了这个问题。 有人能告诉我如何解决上述问题吗? 如果有更好的方法来根据其他列更新列,也很想知道这一点。

谢谢!

I am trying to update a column of a table based on other columns but looks like I cannot do it with the updateWhere function. So I tried to use rawSQl but it does not work due to ambiguous type error.

getCalculateDeltaR :: personId -> Handler Html getCalculateDeltaR personId = do goods <- runDB $ selectList [GoodPerson ==. personId] [] forM goods $ \(Entity gid good) -> do let aid = goodAsset good asset <- runDB $ get404 aid let mktValue = assetMktValue asset runDB $ rawSql "UPDATE good SET delta = (? - orig_value) \ WHERE person = ? AND asset = ?" [toPersistValue mktValue, toPersistValue personId, toPersistValue aid] defaultLayout $ do $(widgetFile "calculateDelta")

I get the following error and I did try adding ?? after UPDATE but it did not make a difference.

Ambiguous type variable `a0' in the constraint: (RawSql a0) arising from a use of `rawSql' Probable fix: add a type signature that fixes these type variable(s) In the second argument of `($)', namely `rawSql ... ...

The parameters that I can passing to rawSQL are well defined so not sure what's causing this issue. Could someone tell me how I should resolve the above issue? If there is a better way to update columns based on other columns, would love to know that as well.

Thanks!

最满意答案

我不知道persistent好,但是这种类型错误的发生是因为你以一种方式组合函数,使得一个值被生成而不被检查。 例如,如果您有函数f :: a -> (Int, c)和g :: (Int, c) -> b则g . f g . f感觉它应该具有类型a -> b ,但它实际上是一个错误,因为GHC不知道c应该是什么。

解决它的方法是以这样的方式检查(Int, c)值,以便可以将c解析为特定类型。 这通常通过手动类型注释或使用asTypeOf来asTypeOf 。

I don't know persistent well, but that type error occurs because you're combining functions in such a way that a value gets produced an consumed without being inspected. For instance, if you had functions f :: a -> (Int, c) and g :: (Int, c) -> b then g . f feels like it ought to have the type a -> b, but it's actually an error because GHC has no idea what c should have been.

The way to solve it is to inspect that (Int, c) value in such a way that c can be resolved to a particular type. This is often done with manual type annotations or by using asTypeOf.

更多推荐

本文发布于:2023-08-06 21:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模糊   错误   类型   RawSql   Update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!