How do you store an enum in Persistent?
Say you have a data State = Ready | Unready | Error
How do you store this in a Persistent database?
This is very simple using derivePersistentField
, as the Yesod docs show. For your case, you could do something like this
-- State.hs
{-# LANGUAGE TemplateHaskell #-}
module State where
import Database.Persist.TH
import Prelude
data State = Ready | Unready | Error
deriving (Show, Read, Eq)
derivePersistField "State"
You can then import this and use it in your model as:
-- Model.hs
import State
SomethingSomething
state State
See the documentation here for an example: https://github.com/yesodweb/yesod/wiki/Persistent-entity-syntax#sum-types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With