Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store an Enum ADT in Persistent

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?

like image 867
user3852278 Avatar asked Jul 18 '14 09:07

user3852278


2 Answers

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
like image 153
bheklilr Avatar answered Oct 22 '22 18:10

bheklilr


See the documentation here for an example: https://github.com/yesodweb/yesod/wiki/Persistent-entity-syntax#sum-types

like image 2
Sebastian Dröge Avatar answered Oct 22 '22 17:10

Sebastian Dröge