UUID in SQLAlchemy

Friday, June 10, 2011

Add a Custom Types

types:

# model/uuid.py
from __future__ import absolute_import
import uuid
from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects.postgresql import UUID


class GUID(TypeDecorator):
    """Platform-independent GUID type.

    Uses Postgresql's UUID type, otherwise uses
    CHAR(32), storing as stringified hex values.

    """
    impl = CHAR

    def load_dialect_impl(self, dialect):
        if dialect.name == 'postgresql':
            return dialect.type_descriptor(UUID())
        else:
            return dialect.type_descriptor(CHAR(32))

    def process_bind_param(self, value, dialect):
        if value is None:
            return value
        elif dialect.name == 'postgresql':
            return str(value)
        else:
            if not isinstance(value, uuid.UUID):
                return "%.32x" % uuid.UUID(value)
            else:
                # hexstring
                return "%.32x" % value

    def process_result_value(self, value, dialect):
        if value is None:
            return value
        else:
            return uuid.UUID(value)

example

mode:

# model/user.py
from you.app.model.uuid import GUID
from you.app.model import meta

class User(meta.BaseObject):

    __tablename__ = "user"

    uuid = schema.Column(GUID(), default=uuid.uuid4, nullable=False, unique=True)

meta:

# model/meta.py
from sqlalchemy import schema
from sqlalchemy.ext.declarative import declarative_base

__all__ = ["Session", "metadata", "BaseObject" ]

Session = None

metadata = schema.MetaData()
BaseObject = declarative_base(metadata=metadata)

reference: Backend-agnostic GUID
Type

This entry was tagged Python, SQLAlchemy and SVN

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top