持久性无法将预期类型“密钥表”与实际类型文本匹配

时间:2022-10-08 17:01:38

i don't know it this is really a problem for you but for me its a big problem. i have a form created in html and one of the field there will hold the the list of city ( it is a select tag) so the value of the option contains the city_id

我不知道这对你来说真的是一个问题,但对我来说这是个大问题。我有一个用html创建的表单,其中一个字段将保存城市列表(它是一个选择标记),因此该选项的值包含city_id

and in my post handler it will look like this:

在我的帖子处理程序中它将如下所示:

 postPriceR :: Handler Html
 postPriceR = do
    now <- liftIO getCurrentTime
    city <- runInputPost $ ireq textField "city"
    amount <- runInputPost $ ireq textField "amount"
    runDB $ insert $ Prices city 100.0 now Nothing
    redirect PriceR

this also will help you: My Models:

这也将对你有所帮助:我的模特:

Prices
    cityId CitiesId
    amount Double
    createdOn UTCTime default=now()
    updatedOn UTCTime Maybe
    deriving Show Generic
Cities
    stateId StatesId
    name Text
    createdOn UTCTime default=now()
    updatedOn UTCTime Maybe
    deriving Show Generic

and give me this warning:

并给我这个警告:

 Couldn't match expected type ‘Key Cities’ with actual type Text …

hope you help me. Thank you.

希望你帮帮我。谢谢。

1 个解决方案

#1


I solve this using @CarstenKönig help. using the toSqlKey

我使用@CarstenKönig帮助解决了这个问题。使用toSqlKey

here it is:

这里是:

import Data.Int

runDB $ insert $ Prices (toSqlKey (read (unpack city) :: Int64 ) ) 100.0 now Nothing

thanks everyone for the help.

谢谢大家的帮助。

#1


I solve this using @CarstenKönig help. using the toSqlKey

我使用@CarstenKönig帮助解决了这个问题。使用toSqlKey

here it is:

这里是:

import Data.Int

runDB $ insert $ Prices (toSqlKey (read (unpack city) :: Int64 ) ) 100.0 now Nothing

thanks everyone for the help.

谢谢大家的帮助。