2

想象一下,您有两个模型(FooBar)并且它们都相互引用(Foo具有barRef类型为BarIdBar具有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’

我应该如何解决这个问题?

4

1 回答 1

2

您用于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 ()

请参阅官方文档以供参考。

于 2018-09-09T18:23:38.700 回答