Better support for type conversions #10

lpsmith opened this Issue Jan 16, 2012 · 7 comments


None yet
2 participants

lpsmith commented Jan 16, 2012

See, for example, #4; we should add support for network addresses and universally unique identifiers among others.

Also, Haskell's UTCTime doesn't support infinity or -infinity, whereas PostgreSQL's timestamp type does.

orclev commented Jan 20, 2013

I'm interested in adding UUID to the list of supported types, pulling in the UUID type from the uuid package on hackage. I'm going to try to fork and add it, but I'm still a bit of a newb with Haskell, so not sure how well that's going to go. Before I spend a bunch of time trying to work out the details of that do you have any objection to pulling in a dependency on uuid?

orclev commented Jan 23, 2013

The work to add support for UUID is mostly done, I just need to add some tests around the new code and then I'll be ready to issue a pull request.


lpsmith commented Jan 23, 2013

Sorry for not responding a little sooner. This is actually a surprisingly common question.

The answer is that you don't need to modify postgresql-simple at all, all you have to do is add a FromField instance for the Haskell type you want to represent UUIDs. I've been meaning to add this exact case as sample code in FromField, actually. Also, you might be interested in looking at the improved FromField documentation in the 0.3 branch.

Here's something that should be pretty close to being correct:

instance FromField UUID where
    fromField f mdata =
        if typeOid f /= builtin2oid UUID
        then returnError Incompatible "" 
        else case B.unpack `fmap` mdata of 
               Nothing   -> returnError UnexpectedNull ""
               Just data -> 
                   case [ x | (x,t) <- reads data, ("","") <- lex t ] of
                     [x] -> Ok x
                     _   -> returnError ConversionError data

Since postgresql's UUID type has a stable oid, we can look at that. For types provided by extensions, such as hstore, you should use the typename operator instead, e.g. if typename f /= "hstore" then ....


lpsmith commented Jan 23, 2013

Also, I will say that I have considered adding out-of-box support for UUIDs, but after consulting some of the postgresql-simple users that use UUIDs we decided to keep postgresql-simple more minimal, with fewer dependencies on this count for the time being.

orclev commented Jan 24, 2013

Wouldn't you also need to create a ToField instance for the UUID type? The work I've done so far was to create a FromField and ToField instance and it seems to work, although I suppose I can see not wanting to pull in the extra dependency just to support UUID. I suppose I could always make the instances a standalone module, something like postgresql-simple-uuid.


lpsmith commented Jan 24, 2013

Depends on your needs, but for whatever reason ToField instances for UUID aren't asked about as often. We should offer both for out-of-box UUID support, though.


lpsmith commented May 8, 2013

Well, there's more work to be done, on this issue, but there's always more work to be done on this issue. I haven't added out-of-box support for network addresses (yet?), and decided against out-of-box support for UUIDs for the time being although I may change my mind, and have included a UUID FromField instance as an explicit example in the documentation. However recent versions do support arrays and hstores, and I have rewritten the time parsers.

So given that this is a fairly generic and on-going issue, I'm going to close it. If anybody wants to request support for another specific type, feel free to open another issue.

@lpsmith lpsmith closed this May 8, 2013

@bergmark bergmark referenced this issue in yesodweb/persistent Sep 13, 2013


persistent-postgresql: Support postgres UUID type #153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment