Haskell "persistent"模型:如何正确定义交叉引用?
Haskell "persistent" model: How to correctly define cross-reference?
假设您有两个模型(Foo 和 Bar)并且它们都相互引用( Foo 有 barRef 类型 BarId 和 Bar 有 fooRef 类型 FooId)。一切顺利:
#!/usr/bin/env stack
{- stack script --resolver=lts-9.21 --package=persistent-template -}
{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell #-}
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Foo
barModel BarId
fooField Int
Bar
fooModel FooId
barField Int
|]
main = pure ()
但是如果我将引用类型包装到 Maybe(只是一个例子,它可以是 Vector 或其他任何类型):
#!/usr/bin/env stack
{- stack script --resolver=lts-9.21 --package=persistent-template -}
{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell #-}
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Foo
barModel (Maybe BarId)
fooField Int
Bar
fooModel FooId
barField Int
|]
main = pure ()
失败并出现此错误:
Not in scope: type constructor or class ‘BarId’
我该如何解决这个问题?
您用于 Bar
模型的语法不正确。这样的事情应该有效:
#!/usr/bin/env stack
-- stack script --resolver lts-12.7
{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell #-}
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Foo
barModel BarId Maybe
fooField Int
Bar
fooModel FooId
barField Int
|]
main = pure ()
假设您有两个模型(Foo 和 Bar)并且它们都相互引用( Foo 有 barRef 类型 BarId 和 Bar 有 fooRef 类型 FooId)。一切顺利:
#!/usr/bin/env stack
{- stack script --resolver=lts-9.21 --package=persistent-template -}
{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell #-}
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Foo
barModel BarId
fooField Int
Bar
fooModel FooId
barField Int
|]
main = pure ()
但是如果我将引用类型包装到 Maybe(只是一个例子,它可以是 Vector 或其他任何类型):
#!/usr/bin/env stack
{- stack script --resolver=lts-9.21 --package=persistent-template -}
{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell #-}
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Foo
barModel (Maybe BarId)
fooField Int
Bar
fooModel FooId
barField Int
|]
main = pure ()
失败并出现此错误:
Not in scope: type constructor or class ‘BarId’
我该如何解决这个问题?
您用于 Bar
模型的语法不正确。这样的事情应该有效:
#!/usr/bin/env stack
-- stack script --resolver lts-12.7
{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell #-}
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Foo
barModel BarId Maybe
fooField Int
Bar
fooModel FooId
barField Int
|]
main = pure ()