1

Imagine you have two models (Foo and Bar) and they are both have references to each other (Foo have barRef with type BarId and Bar have fooRef with type FooId). Everything goes okay:

#!/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 ()

But if I wrap reference type to Maybe (just an example, it could Vector or anything else):

#!/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 ()

It fails with this error:

Not in scope: type constructor or class ‘BarId’

How I am supposed to solve this?

Norrius
  • 7,558
  • 5
  • 40
  • 49
unclechu
  • 821
  • 10
  • 14

1 Answers1

1

The syntax you are using for the Bar model isn't right. Something like this should work:

#!/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 ()

See the official docs for reference.

Sibi
  • 47,472
  • 16
  • 95
  • 163
  • I'm actually faced this issue using **Vector**, for some reason I have thought this syntax is only for **Maybe**, like just for nullable fields. But I realized now that this is general syntax for any wrapper in reverse order. So it could be even `barModel BarId Vector Maybe` for `barModel :: Maybe (Vector (BarId))`. Thanks! – unclechu Sep 09 '18 at 18:43
  • @unclechu I'm not sure something like that will work out. Test that out and let us know. – Sibi Sep 09 '18 at 18:45
  • I was wrong, this doesn't work like that, `Maybe` is just a special case. – unclechu Nov 29 '18 at 23:48