so my problem is i am trying to make an enum with int values but it didn’t work.
first of all, this is the code.
class UserRole(enum.IntEnum):
publisher = 4
editor = 3
manager = 2
admin = 1
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, nullable=False, index=True)
username = Column(String, unique=True, nullable=False, index=True)
hashed_password = Column(String, nullable=False)
user_role = Column(Enum(UserRole), default=UserRole.reader)
now when i create user, go to see user_role i found it admin not 1
i though it might be a cache so i deleted all cache and recreated the database.
until i tried to change one of variables and found that the values of enum that stored on database are the variables itself not the value.
please help me how i can store the enum data as an int on database.
my last attempt was
class UserRole(enum.IntEnum):
reader: int = 7
Pro_reader: int = 6
publisher: int = 5
editor: int = 4
small_manager: int = 3
big_manager: int = 2
admin: int = 1
and didn’t work.
2
Answers
normally, enum accept string only. so to change it i have to made a custom type and bellow is the code for it
custom_types.py
add custom type to models.py
also i notice that user_role field on database is stored as integer not as enum.
another sloution i didn't try and don't know if it will work was ChoiceType from SQLAlchemy-Utils library.
To use values of Enum you need set values_callable parameter:
Look up bottom of this section in SQLAlchemy docs. You will found: