Search code snippets, questions, articles...

Create Enums and use them in SQLAlchemy

In this post, we are going to explain to create Enums in Python and use them to store and retrieve them from the database using SQLAlchemy.
from database import db
import enum

class AnswerType(enum.Enum):
    no = 'No'
    yes = 'Yes'
    dont_know = 'Do not Know'

    def __str__(self):
        return self.value

class Answers_Model(db.Model):
    __tablename__ = "answers"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, mssql_identity_start=1000)

    answer_type = db.Column(db.Enum(ReportTypes), nullable=False)
Was this helpful?