[Haskell-cafe] Problem using Persistent

Roel van Dijk vandijk.roel at gmail.com
Sun Jan 26 09:59:26 UTC 2020


I'm not familiar with persistent, but I might have some ideas.
You are using template haskell. So while you are not directly using json,
you are using it indirectly, via the code generated by mkPersist [1].
Presumably mkPersist must create an PersistEntity Account instance. Looking
at the superclass constraints of PersistEntity [2] we see ToJSON (Key
record) and FromJSON (Key record) where the record type variable is you
Account type.
You can easily create instances of FromJSON and ToJSON by using Generics
[3]. But this only works if you type is an instance of the Generic type
class. I think that the code generated by mkPersist depends on the Key
Account type having an instance for Generic. Notice that Key is a data
family (type level function) and thus Key Account equals some other type.

I can really recommend the -ddump-splices GHC option [4] when debugging
code involving template haskell. It is a lot easier to understand a problem
when you can just read the generated Haskell code.

1 -
https://hackage.haskell.org/package/persistent-template-2.8.0.1/docs/Database-Persist-TH.html#v:mkPersist
2 -
https://hackage.haskell.org/package/persistent-2.10.4/docs/Database-Persist-Class.html#t:Persistently
3 -
https://hackage.haskell.org/package/aeson-1.4.6.0/docs/Data-Aeson.html#v:parseJSON
4 -
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/debugging.html#ghc-flag--ddump-splices

Op vr 17 jan. 2020 om 18:29 schreef Debasish Ghosh <ghosh.debasish at gmail.com
>:

> Hi -
>
> I have an entity declaration in persistent as follows:
>
> share [mkPersist sqlSettings { mpsGenerateLenses = True, mpsPrefixFields =
> False }, mkMigrate "migrateAll"] [persistLowerCase|
>     Account
>         accountNo           Text
>         accountType         AccountType
>         accountHolderName   Text
>         accountOpenDate     UTCTime default=CURRENT_TIME
>         accountCloseDate    UTCTime Maybe default=NULL
>         currentBalance      MoneyUSD
>         rateOfInterest      Double
>         Primary             accountNo
>         deriving Show
> |]
>
> I am getting this compilation error ..
>
> • No instance for (Generic (Key Account))
>         arising from the 'deriving' clause of a data type declaration
>       Possible fix:
>         use a standalone 'deriving instance' declaration,
>           so you can specify the instance context yourself
>     • When deriving the instance for
> (aeson-1.4.6.0:Data.Aeson.Types.ToJSON.ToJSON
>                                         (Key Account))
>    |
> 40 | share [mkPersist sqlSettings { mpsGenerateLenses = True,
> mpsPrefixFields = False }, mkMigrate "migrateAll"] [persistLowerCase|
>    |
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
>
> and another exactly similar for FromJSON
>
> What exactly is going wrong here ? I am not using json in the above
> definition.
>
> regards.
> --
> Debasish Ghosh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200126/c9c9c873/attachment.html>


More information about the Haskell-Cafe mailing list