如果在Postgresql下使用随机字符串在SQLAlchemy模型行中创建?

时间:2013-11-21 17:59:52

标签: python postgresql sqlalchemy

当Postgresql在?

下时,如何使用随机字符串在SQLAlchemy模型行中创建?

我需要从MySQL转换,我可以这样做。

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

import uuid

class GUID(types.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)

0 个答案:

没有答案