我知道,这个问题已经在往昔中提出,现在标记为已解决。
我使用的是postgresql-simple包和Data.Time。如果我进行查询以检索类型为timestamptz的单个列,则会得到模式匹配错误。伪码如下:
import Data.Text as T
import Data.Time (UTCTime,LocalTime,ZonedTime)
import Database.PostgreSQL.Simple as Pg
import Database.PostgreSQL.Simple.FromRow
import Database.PostgreSQL.Simple.ToRow
import Database.PostgreSQL.Simple.ToField
import Database.PostgreSQL.Simple.Time
import qualified Data.Vector as V
...
--- in main this is the code
[Pg.Only (i::UTCTime)] <- Pg.query conn "select lastupdated from constraints where name=?" ( Only ("carrier"::T.Text))
...在执行查询时,我会在运行时得到此错误(请注意:
*** Exception: user error (Pattern match failure in do expression at ...)如果上面更改为无效类型,如下所示:
[Pg.Only (i::T.Text)] <- Pg.query conn "select lastupdated from constraints where name=?" ( Only ("carrier"::T.Text))我得到了关于类型的异常,这表明所期望的类型确实是timestamptz
*** Exception: Incompatible {errSQLType = "timestamptz",...,
errSQLField = "lastupdated", errHaskellType = "Text", errMessage = "types incompatible"}此外,上述查询结果在psql控制台中的外观如下:
lastupdated
-------------------------------
2016-04-13 00:08:33.789761+00
2016-04-13 14:33:38.27739+00
(2 rows)我的理解是,Data.Time.UTCTime或Database.PostgreSQL.Simple.Time.UTCTimestamp应该映射到timestamptz类型的PostgreSQL。显然,这种理解是错误的,如果我把这些错误放在上面。在这方面会很感激的。
发布于 2016-04-14 21:36:48
试一试:
(times :: [Pg.Only UTCTime]) <-
Pg.query conn "select lastupdated from constraints where name=?" (Only ("carrier" :: T.Text))您的模式匹配不仅强制要求有一个Only UTCTime的列表,而且还要求列表中只有一个元素。由于您有两条记录,query将返回一个包含两个元素的列表,模式匹配失败。
https://stackoverflow.com/questions/36633959
复制相似问题