在 Sqlalchemy 中进行枚举的最佳方法? [英] Best way to do enum in Sqlalchemy?
本文介绍了在 Sqlalchemy 中进行枚举的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读有关 sqlalchemy 的文章,我看到了以下代码:
I'm reading about sqlalchemy and I saw following code:
employees_table = Table('employees', metadata,
Column('employee_id', Integer, primary_key=True),
Column('name', String(50)),
Column('manager_data', String(50)),
Column('engineer_info', String(50)),
Column('type', String(20), nullable=False)
)
employee_mapper = mapper(Employee, employees_table, \
polymorphic_on=employees_table.c.type, polymorphic_identity='employee')
manager_mapper = mapper(Manager, inherits=employee_mapper, polymorphic_identity='manager')
engineer_mapper = mapper(Engineer, inherits=employee_mapper, polymorphic_identity='engineer')
我应该在库中使用常量将类型"设为 int 吗?或者我应该让 type 成为一个枚举?
Should I make 'type' an int, with constants in a library? Or should I make just make type an enum?
推荐答案
SQLAlchemy 从 0.6 开始就有了 Enum 类型:http://docs.sqlalchemy.org/en/latest/core/type_basics.html?highlight=enum#sqlalchemy.types.Enum
SQLAlchemy has an Enum type since 0.6: http://docs.sqlalchemy.org/en/latest/core/type_basics.html?highlight=enum#sqlalchemy.types.Enum
尽管我只建议在您的数据库具有本机枚举类型时使用它.否则我个人只会使用 int.
Although I would only recommend its usage if your database has a native enum type. Otherwise I would personally just use an int.
这篇关于在 Sqlalchemy 中进行枚举的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文