[web-devel] problem with yesod, persist and enumerations

Chris Casinghino chris.casinghino at gmail.com
Fri Apr 15 17:27:19 CEST 2011


Hi all,

I'm running into significant compile-time performance problems with a
relatively simple database.  This email describes a small example
using a default project from the yesod scaffolding tool.  Trying to
compile the project causes ghc to consume several GB of memory on my
system (eventually I have to kill it).  I'm running GHC 7.0.3 and the
current yesod beta.  It's definitely possible that I'm doing something
very stupid - any help would be appreciated.

The example just involves adding one table to a default project's
database.  Suppose I want to add a table with 15 columns, each holding
a US State or Nothing (a little silly, but in my real application the
data is more interesting).  To do this, I'll make a datatype to list
the US states, give it a PersistField instance with some help from the
Enum typeclass, and add the table.  In the Model.hs file, I add an
import line:

> import Database.Persist.Base

And then I define my representation of the US State codes:

> data USStateCode =
>     AL | AK | AS | AZ | AR | CA | CO | CT | DE | DC
>   | FM | FL | GA | GU | HI | ID | IL | IN | IA | KS | KY | LA | ME
>   | MD | MA | MI | MN | MS | MO | MT | NE | NV | NH | NJ | NM | NY
>   | NC | ND | MP | OH | OK | OR | PW | PA | PR | RI | SC | SD | TN
>   | TX | UT | VT | VI | VA | WA | WV | WI | WY
>   deriving (Read, Show, Eq, Ord, Enum)
>
> instance PersistField USStateCode where
>   toPersistValue = PersistInt64 . toEnum . fromEnum
>
>   fromPersistValue (PersistInt64 a) = Right $ toEnum $ fromEnum a
>   fromPersistValue _ = Left "error"
>
>   sqlType _ = SqlInteger


And I add the table to the bottom of my entities file:

> MyTable
>     st1 USStateCode Maybe
>     st2 USStateCode Maybe
>     st3 USStateCode Maybe
>     st4 USStateCode Maybe
>     st5 USStateCode Maybe
>     st6 USStateCode Maybe
>     st7 USStateCode Maybe
>     st8 USStateCode Maybe
>     st9 USStateCode Maybe
>     st10 USStateCode Maybe
>     st11 USStateCode Maybe
>     st12 USStateCode Maybe
>     st13 USStateCode Maybe
>     st14 USStateCode Maybe
>     st15 USStateCode Maybe

Making these changes to a default project and then compiling causes
the problems I described above.  Can someone fill me on on a better
way to add a table holding some enumerated data?

Thanks!

--Chris



More information about the web-devel mailing list