gpt4 book ai didi

yesod - 自定义主键持久化 `toSqlKey`

转载 作者:行者123 更新时间:2023-12-02 20:36:40 27 4
gpt4 key购买 nike

我正在定义以下模型,其中Category具有Text类型的非整数主键code

Category
code Text
Primary code
deriving Show Typeable

CategoryTreeNode
name Text
code CategoryId Maybe
lft Int MigrationOnly default=0
rgt Int MigrationOnly default=0
deriving Show Typeable

对于没有 Primary 定义的模型,我可以使用 toSqlKey 将任意 Int64 转换为 Key Category。对于具有自定义主键的模型是否有等效的函数?

最佳答案

我通过查看 persistence 的 TH 模块找到了答案:

https://github.com/yesodweb/persistent/blob/9396c278fc181fdac4a97c53637700417f41a478/persistent-template/Database/Persist/TH.hs#L857-L858

这一行表明 CategoryKey 是通过 TH 生成的,事实确实如此:

let x :: Key Category
x = CategoryKey "foobar"

关于yesod - 自定义主键持久化 `toSqlKey`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901164/

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