gpt4 book ai didi

python - SQLAlchemy 枚举代码表示法和数据库表示法

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:41 25 4
gpt4 key购买 nike

这是对best-way-to-do-enum-in-sqlalchemy非常有趣的答案的后续。 。 Cito 扩展了 Zzzeeks 的答案,包括订购,这非常好。 Cito 还在末尾留下了一段诱人的代码。

class DeclEnumType(SchemaType, TypeDecorator):
"""DeclEnum augmented so that it can persist to the database."""

这正是我试图构建一个 Python 枚举,该枚举在数据库中自己的表中表示。其中 EmployeeType.full_time 可在 Python 代码中使用,并且在数据库中拥有自己的表(对于这个简单的示例,只有 idx 和名称)。

但是,我不确定我是否理解如何使用 Cito 的 DeclEnumType 示例,因为以下不会在数据库中创建 EmployeeType 表。

class EmployeeType(DeclEnum):
# order will be as stated: full_time, part_time, contractor
full_time = EnumSymbol("Full Time")
part_time = EnumSymbol("Part Time")
contractor = EnumSymbol("Contractor")

class Employee(Base):
__tablename__ = 'employee'

id = Column(Integer, primary_key=True)
name = Column(String(60), nullable=False)
type = Column(DeclEnumType(EmployeeType))

关于如何获得这种双重表示有什么想法吗?

最佳答案

如果我没记错的话,正在做:

type = Column(EmployeeType.db_type())

而不是

type = Column(DeclEnumType(EmployeeType))

应该这样做。

关于python - SQLAlchemy 枚举代码表示法和数据库表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15033177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com