如何在SQLAlchemy中使用UUID?

时间:2008-10-08 14:26:21

标签: python postgresql orm sqlalchemy uuid

如果使用UUID(Postgres),有没有办法在SQLAlchemy中将列(主键)定义为PostgreSQL

9 个答案:

答案 0 :(得分:63)

sqlalchemy postgres方言支持UUID列。这很容易(问题特别是postgres) - 我不明白为什么其他答案都那么复杂。

以下是一个例子:

tell application "System Events" to get bundle identifier of application process "Growl"

答案 1 :(得分:60)

I wrote this域名已经消失,但这是胆量......

无论我真正关心正确数据库设计的同事如何看待用于关键字段的UUID和GUID。我经常发现我需要这样做。我认为它比自动增量有一些优势使它值得。

过去几个月我一直在改进UUID列类型,我想我终于搞定了。

from sqlalchemy import types
from sqlalchemy.dialects.mysql.base import MSBinary
from sqlalchemy.schema import Column
import uuid


class UUID(types.TypeDecorator):
    impl = MSBinary
    def __init__(self):
        self.impl.length = 16
        types.TypeDecorator.__init__(self,length=self.impl.length)

    def process_bind_param(self,value,dialect=None):
        if value and isinstance(value,uuid.UUID):
            return value.bytes
        elif value and not isinstance(value,uuid.UUID):
            raise ValueError,'value %s is not a valid uuid.UUID' % value
        else:
            return None

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

    def is_mutable(self):
        return False


id_column_name = "id"

def id_column():
    import uuid
    return Column(id_column_name,UUID(),primary_key=True,default=uuid.uuid4)

# Usage
my_table = Table('test',
         metadata,
         id_column(),
         Column('parent_id',
            UUID(),
            ForeignKey(table_parent.c.id)))

我认为存储为二进制(16字节)应该最终比字符串表示更有效(36字节?),并且似乎有一些迹象表明索引16字节块在mysql中应该比字符串更有效。不管怎样,我不认为它会更糟。

我发现的一个缺点是,至少在phpymyadmin中,你无法编辑记录,因为它隐式地尝试为“select * from table where id = ...”进行某种字符转换,并且有杂项显示问题。

除此之外一切似乎都很好,所以我把它扔出去了。如果你看到一个明显的错误,请发表评论。我欢迎任何改进它的建议。

除非我遗漏了某些内容,否则如果底层数据库具有UUID类型,则上述解决方案将起作用。如果没有,则在创建表时可能会出错。我提出的解决方案是我最初的目标是MSSqlServer,最后是MySql,所以我认为我的解决方案更灵活,因为它似乎在mysql和sqlite上工作正常。还没有打扰检查postgres。

答案 2 :(得分:30)

另请参阅SQLAlchemy文档中有关列类型的Backend-agnostic GUID Type的配方。

答案 3 :(得分:13)

如果您对“String'具有UUID值的列,这里是一个简单的解决方案:

def generate_uuid():
    return str(uuid.uuid4())

class MyTable(Base):
    __tablename__ = 'my_table'

    uuid = Column(String, name="uuid", primary_key=True, default=generate_uuid)

答案 4 :(得分:11)

答案 5 :(得分:4)

这是一种基于SQLAlchemy文档中Backend agnostic GUID的方法,但使用BINARY字段将UUID存储在非postgresql数据库中。

import uuid

from sqlalchemy.types import TypeDecorator, BINARY
from sqlalchemy.dialects.postgresql import UUID as psqlUUID

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

    Uses Postgresql's UUID type, otherwise uses
    BINARY(16), to store UUID.

    """
    impl = BINARY

    def load_dialect_impl(self, dialect):
        if dialect.name == 'postgresql':
            return dialect.type_descriptor(psqlUUID())
        else:
            return dialect.type_descriptor(BINARY(16))

    def process_bind_param(self, value, dialect):
        if value is None:
            return value
        else:
            if not isinstance(value, uuid.UUID):
                if isinstance(value, bytes):
                    value = uuid.UUID(bytes=value)
                elif isinstance(value, int):
                    value = uuid.UUID(int=value)
                elif isinstance(value, str):
                    value = uuid.UUID(value)
        if dialect.name == 'postgresql':
            return str(value)
        else:
            return value.bytes

    def process_result_value(self, value, dialect):
        if value is None:
            return value
        if dialect.name == 'postgresql':
            return uuid.UUID(value)
        else:
            return uuid.UUID(bytes=value)

答案 6 :(得分:3)

如果有人感兴趣,我一直在使用Tom Willis的答案,但发现在process_bind_param方法中为uuid.UUID转换添加字符串很有用

class UUID(types.TypeDecorator):
    impl = types.LargeBinary

    def __init__(self):
        self.impl.length = 16
        types.TypeDecorator.__init__(self, length=self.impl.length)

    def process_bind_param(self, value, dialect=None):
        if value and isinstance(value, uuid.UUID):
            return value.bytes
        elif value and isinstance(value, basestring):
            return uuid.UUID(value).bytes
        elif value:
            raise ValueError('value %s is not a valid uuid.UUId' % value)
        else:
            return None

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

    def is_mutable(self):
        return False

答案 7 :(得分:0)

由于您使用的是Postgres,因此应该可以:

from app.main import db
from sqlalchemy.dialects.postgresql import UUID

class Foo(db.Model):
    id = db.Column(UUID(as_uuid=True), primary_key=True)
    name = db.Column(db.String, nullable=False)

答案 8 :(得分:-18)

您可以尝试编写custom type,例如:

import sqlalchemy.types as types

class UUID(types.TypeEngine):
    def get_col_spec(self):
        return "uuid"

    def bind_processor(self, dialect):
        def process(value):
            return value
        return process

    def result_processor(self, dialect):
        def process(value):
            return value
        return process

table = Table('foo', meta,
    Column('id', UUID(), primary_key=True),
)