[Haskell-cafe] Using Haskell types in a database

Nikita Karetnikov nikita at karetnikov.org
Tue Feb 10 18:02:58 UTC 2015


> That part I don't understand.  Why does your proposal require varchars
> everywhere?

That's what Persistent generates (using derivePersistField) for the
second table.  For example, the Haskell value

MyText "foo"

where MyText is

newtype MyText = MyText Text deriving (Show, Read)
derivePersistField "MyText"

will be stored in PostgreSQL as a varchar

'MyText "foo"'

I assume that Persistent (or its PostgreSQL backend) has a predefined
mapping from a commonly used Haskell types to the database types.
And everything else is represented as a varchar.

So there are two choices, corresponding to the two tables shown in the
original message:

1. Design tables with the (basic) database types in mind (integer,
   boolean, timestamp, varchar, etc.) then wrap and unwrap values each
   time you interact with the database.

2. Use Haskell types like MyText or something more complex directly
   and end up with varchar everywhere.

I wonder how bad is the second option in practice because
wrapping/unwrapping is error-prone.  Of course, the varchar thing makes
the alarm bells ring in my head, but when does it significantly affect
performance?  For example, maybe it's okay for a small shop, or is it a
no go for everyone?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20150210/7f43e9d4/attachment.sig>


More information about the Haskell-Cafe mailing list