2

I believe this question is more related to the Haskell structure itself than Persistent's, but I've been looking through some questions here and I stumbled upon this.

Now, I wonder, is there a way to use a type generated through mkPersist inside a custom type generated through derivePersistField? Now, taking the following,

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Model where

import Database.Persist.TH
import Data.Text
import Extra

share
    [ mkPersist sqlSettings
    , mkMigrate "migrateAll"
    , mkDeleteCascade sqlSettings
    ] [persistLowerCase|
        User
            ident    Text
            passwd   Text
            person   Person
        Regular
            name     Text
            username Text
            birth    Day
        Admin
            authLvl  Int
    |]
{-# LANGUAGE TemplateHaskell #-}

module Extra where

import Database.Persist.TH

data Person = PersonRegular | PersonAdmin
    deriving (Show, Read, Eq)
derivePersistField "Person"

The problem is, Extra needs to be imported in Model, that is fine; however, If I were to use the RegularId, from the Regular table, inside the Person type,

data Person = PersonRegular RegularId | PersonAdmin AdminId

I would also need to import Model in Extra, which leads to an import cycle.

Is there a way to fix that and achieve my objective? To put everything in the same module is ruled out, since it'll throw me a GHC stage restriction. I also thought about using {-# SOURCE #-} and hs-boot, but for some reason stack still complained about import cycle, but with the hs-boot file instead.

Norrius
  • 7,558
  • 5
  • 40
  • 49
ptkato
  • 668
  • 1
  • 7
  • 14
  • Can you turn your hypothetical into a concrete snippet of code that doesn't work but that you wish would, please? The commentary on the stage restriction and stack is great (it shows effort!) but should probably be extracted into their own, separate questions, one for each, that again follows the usual [MCVE formula](https://stackoverflow.com/help/mcve) of including enough code that we can reproduce your error. Feel free to link them from this question to preserve your evidence of effort. – Daniel Wagner Dec 07 '18 at 18:35

0 Answers0