我正在使用Scotty和PostgreSQL-simple开发一个Haskell API。我不知道如何插入包含文本数组的列。到目前为止,我的查询已经起作用了,但是关于这个查询的一些东西似乎没有很好地编译。
这就是我所拥有的:
addOrder :: Postgres r m => OrderIntent -> m ()
addOrder param =
void . withConn $ \conn -> execute conn qry (orderIntentTable param, orderIntentItemsSlug param)
where
qry = "insert into orders (table, items, created_at, updated_at) values (?, ?, now(), now())"我的OrderIntent及其FromRow是:
data OrderIntent = OrderIntent
{ orderIntentTable :: Integer
, orderIntentItemsSlug :: [Text]
} deriving(Eq, Show)
instance FromRow OrderIntent where
fromRow = OrderIntent
<$> field
<*> (fromPGArray <$> field)我得到的错误是:
• Could not deduce (Database.PostgreSQL.Simple.ToField.ToField
[Data.Text.Internal.Text])
arising from a use of ‘execute’
from the context: Postgres r m
bound by the type signature for:
addOrder :: forall r (m :: * -> *).
Postgres r m =>
OrderIntent -> m ()
at Core/Order/DAO.hs:11:1-47我不知道如何使用PostgreSQL库来解析文本数组,以便将我插入到数据库中。如果你们任何人能帮助我,我将不胜感激!
PS:我也有几天前的this other blocker,如果你碰巧知道关于Scotty auth的话。
发布于 2021-02-03 22:58:26
使用PGArray。
execute conn qry (orderIntentTable param, PGArray (orderIntentItemsSlug param))发布于 2021-02-03 22:46:25
在插入orderIntentItemsSlug时,需要一个orderIntentItemsSlug的ToField实例(这是一个[Text])。这就是你的错误告诉你的。
我建议查看其他ToField instances的源代码,以弄清楚您想要如何做到这一点(一种老生常谈的方法是首先将您的字段转换为Text,这已经为ToField实例生成了气体)。
您还可以编写一个orders表,一次将一个orderID与一个商品关联起来,这样就不需要编写任何实例了。
https://stackoverflow.com/questions/66029678
复制相似问题