ENUM type in SQLAlchemy with PostgreSQL

前端 未结 4 1990
我寻月下人不归
我寻月下人不归 2020-12-24 07:48

I\'m using SQLAlchemy core with a postgresql database and I would like to add the ENUM type to my table definition. According to the postgresql documentatio

4条回答
  •  春和景丽
    2020-12-24 08:29

    You can use Python's native enums as the column type too:

    from flask_sqlalchemy import SQLAlchemy
    sqlalchemy.dialects.postgresql import ENUM as pgEnum
    from enum import Enum, unique
    
    @unique
    class errorTypeEnum(Enum):
        videoValidation = 'videoValidation'
        audioValidation = 'audioValidation'
        subtitleValidation = 'subtitleValidation'
    
    db = SQLAlchemy()
    
    class Error(db.Model):
        serviceID = db.Column(db.String(20), primary_key=True)
        timestamp = db.Column(db.DateTime, unique=False, nullable=False)
        category = db.Column(pgEnum(errorTypeEnum), unique=False, nullable=False)
    

提交回复
热议问题