我正在为所有表格使用新的键
newtype Key' a = Key a deriving (Show, Generic, Functor)
type Key = Key' Int64
type KeyR = Key' (Column PGInt8)
type KeyW = Key' (Maybe (Column PGInt8))
$(makeAdaptorAndInstance "pKey" ''Key')
我现在想在另一张桌子上对这样一个键进行可空的引用,但我正在努力。如何将密钥引用为可空?
答案 0 :(得分:0)
Opaleye有一个Nullable PGInt8
到Maybe Int64
的默认实例。
您要做的是从Key' (Column (Nullable PGInt8))
转换为Maybe Key = Maybe (Key' Int64)
。
要使默认实例起作用,您需要转换
从Key' (Column (Nullable PGInt8)))
到Key' (Maybe Int64)
从Column (Nullable (Key' PGInt8))
到Maybe Key = Maybe (Key' Int64)
答案 1 :(得分:0)
将我的评论与@ ForestPhoenix的答案结合起来:
Opaleye允许您在运行查询时将Column PGInt8
转换为Int64
。
这意味着 允许您将Column (Nullable PGInt8)
转换为Maybe Int64
。
您将Column (Nullable PGInt8)
包裹在Key'
新类型(用于类型安全)中,它会为您提供Key' (Column (Nullable PGInt8))
。这意味着当您运行查询时,您需要将其读作Key' (Maybe Int64)
。
也许这张小桌子使通信更加清晰:
Opaleye side | Haskell side
----------------------------- | -------------
Column PGInt8 | Int64
Column (Nullable PGInt) | Maybe Int64
Key' (Column (Nullable PGInt) | Key' (Maybe Int64)
一般来说:
Opaleye side | Haskell side
------------------------------- | -------------
Column o | h
Column (Nullable o) | Maybe h
MyNewype' (Column (Nullable o)) | MyNewType' (Maybe h)