I have Python Enum
class like this:
from enum import Enum
class Seniority(Enum):
Intern = "Intern"
Junior_Engineer = "Junior Engineer"
Medior_Engineer = "Medior Engineer"
Senior_Engineer = "Senior Engineer"
In MYSQL database, seniority ENUM column has values "Intern", "Junior Engineer", "Medior Engineer", "Senior Engineer".
The problem is that I get an error:
LookupError: "Junior Engineer" is not among the defined enum values
This error has occurred when I call query like:
UserProperty.query.filter_by(full_name='John Doe').first()
seniority
is enum property in the UserProperty
model.
class UserProperty(db.Model):
...
seniority = db.Column(db.Enum(Seniority), nullable=True)
...
For this class I've defined Schema class using marshmallow
Schema
and EnumField
from marshmallow_enum
package:
class UserPropertySchema(Schema):
...
seniority = EnumField(Seniority, by_value=True)
...
What to do in this situation, because I can't define python class property name with space. How to force python to use values of defined properties instead of property names?
As Shenanigator stated in the comment of my question, we can use aliases to solve this problem.
Seniority = Enum(
value='Seniority',
names=[
('Intern', 'Intern'),
('Junior Engineer', 'Junior Engineer'),
('Junior_Engineer', 'Junior_Engineer'),
('Medior Engineer', 'Medior Engineer'),
('Medior_Engineer', 'Medior_Engineer'),
('Senior Engineer', 'Senior Engineer'),
('Senior_Engineer', 'Senior_Engineer')
]
)