gpt4 book ai didi

haskell - JSONify 持久化模型 *包括* id

转载 作者:行者123 更新时间:2023-12-02 12:18:13 24 4
gpt4 key购买 nike

这是我在 Persistent 中的模型:

Tip
text Text
created_at UTCTime
updated_at UTCTime
title Text
category_id CategoryId

以及相关的ToJSON实例(我无法使用deriveJSON):

instance ToJSON Tip where
toJSON (Tip text created_at updated_at title category_id) = object ["text" .= text, "created_at" .= created_at, "updated_at" .= updated_at, "title" .= title, "category_id" .= category_id]

这几乎是正确的,除了我也想在此处对提示的 id 进行 JSON 化...但它不在模型上的任何位置!我该怎么做?有没有办法从 EntityValEntityKey

最佳答案

实体是键和值的配对。如果您只有值,则没有通用方法可以知道它的 id。如果它有任何独特的约束,您可以使用它来查找 key 。

理论上,给定的 EntityVal 可以在不同的后端数据库中分配不同的 ID(甚至可能不具有相同的类型)。或者它可能已手动构造为要插入,但实际上尚未插入,因此尚未为其分配 id。 Tip 值本身并不拥有 id;它可能只在特定数据库中有一个 id。

关于haskell - JSONify 持久化模型 *包括* id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com