MCPcopy Create free account
hub / github.com/LazyAGI/LazyCraft / StringUUID

Class StringUUID

back/src/models/__init__.py:19–192  ·  view source on GitHub ↗

字符串格式的 UUID 数据类型。 这是一个 SQLAlchemy 自定义类型,用于在数据库中存储 UUID 值。 支持多种数据库后端,自动处理不同格式之间的转换。 特性: - 支持 MySQL、PostgreSQL、SQLite 等多种数据库 - 自动处理 UUID 对象、字符串、None 值的转换 - 统一的 36 字符长度存储格式(带连字符) - 高性能缓存支持 - 完整的错误处理和日志记录 存储格式: - MySQL: CHAR(36) - "550e8400-e29

Source from the content-addressed store, hash-verified

17
18
19class StringUUID(TypeDecorator):
20 """
21 字符串格式的 UUID 数据类型。
22
23 这是一个 SQLAlchemy 自定义类型,用于在数据库中存储 UUID 值。
24 支持多种数据库后端,自动处理不同格式之间的转换。
25
26 特性:
27 - 支持 MySQL、PostgreSQL、SQLite 等多种数据库
28 - 自动处理 UUID 对象、字符串、None 值的转换
29 - 统一的 36 字符长度存储格式(带连字符)
30 - 高性能缓存支持
31 - 完整的错误处理和日志记录
32
33 存储格式:
34 - MySQL: CHAR(36) - "550e8400-e29b-41d4-a716-446655440000"
35 - PostgreSQL: 优先使用原生 UUID 类型,回退到 CHAR(36)
36 - 其他数据库: CHAR(36)
37
38 使用示例:
39 class User(Base):
40 id = Column(StringUUID(), primary_key=True, default=lambda: str(uuid.uuid4()))
41 name = Column(String(255))
42 """
43
44 # 指定基础实现类型
45 impl = CHAR
46
47 # 允许 SQLAlchemy 缓存这个类型,提高查询性能
48 cache_ok = True
49
50 def __init__(self, length: int = 36):
51 """
52 初始化 StringUUID 类型。
53
54 Args:
55 length (int): 存储长度,默认 36 字符(标准 UUID 格式)
56 """
57 self.length = length
58 super().__init__()
59
60 def load_dialect_impl(self, dialect):
61 """
62 根据不同的数据库方言加载适当的类型实现。
63
64 为不同的数据库选择最优的存储类型:
65 - PostgreSQL: 优先使用原生 UUID 类型
66 - MySQL: 使用 MySQL 优化的 CHAR 类型
67 - 其他: 使用标准 CHAR 类型
68
69 Args:
70 dialect: SQLAlchemy 数据库方言对象
71
72 Returns:
73 适合当前数据库的类型描述符
74 """
75 if dialect.name == "postgresql":
76 # PostgreSQL 有原生 UUID 支持,性能更好

Callers 1

upgradeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected