我定义了一个PostgreSQL表,如下所示:
> \d+ public."Users"
Table "public.Users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
-------------+--------------------------+-----------+----------+-------------------------------------+----------+-------------+--------------+-------------
id | integer | | not null | nextval('"Users_id_seq"'::regclass) | plain | | |
username | character varying(255) | | not null | | extended | | |
userId | character varying(255) | | | | extended | | |
我尝试使用postgresql-simple
查询该表:
maybeUsername <- liftIO (listToMaybe <$> query conn "SELECT username FROM public.\"Users\" WHERE \"userId\" = ?" (Only userId) :: IO (Maybe String))
case maybeUsername of
Just username -> liftIO $ putStrLn $ "Username is " ++ username
Nothing -> liftIO $ putStrLn $ "Could not find a record for userId " ++ userId
由于某种原因,我得到了以下错误:
Incompatible {errSQLType = "varchar", errSQLTableOid = Just (Oid 24782), errSQLField = "username", errHaskellType = "Char", errMessage = ""}
有办法解决吗?
1条答案
按热度按时间5fjcxozz1#
当返回单个列时,库很难推断出类型(记录在这里:https://hackage.haskell.org/package/postgresql-simple-0.6.5/docs/Database-PostgreSQL-Simple.html#g:4)。因此,我们需要通过执行
Only String
而不是String
来解决这个问题。这样它就像预期的那样工作: