[Psycopg] psycopg2 adaptation for UUIDs

Brian Sutherland brian at vanguardistas.net
Tue Sep 16 10:51:06 CEST 2008


Hi,

Seeing as both PostgreSQL 8.3 and python 2.5 support the UUID data type,
I thought it might be a good feature if psycopg2 supported them natively
as well. (don't know what project policy is on this)

This is the code I am using to be able to put UUID object into and get
them out of PostgreSQL:

    try:
        # uuid is included in python2.5
        import uuid
        UUID = psycopg2.extensions.new_type((2950, ), "UUID", lambda s, cursor: uuid.UUID(s))
        psycopg2.extensions.register_type(UUID)
        psycopg2.extensions.register_adapter(uuid.UUID, lambda x: psycopg2.extensions.adapt(str(x)))
    except ImportError:
        pass

-- 
Brian Sutherland


More information about the Psycopg mailing list