定义模型时枚举字段中的默认值错误 class
Error with default value in enum field when define model class
您好,我有以下型号:
class Status(Enum):
scheduled: 'scheduled'
recording: 'recording'
complete: 'complete'
failed: 'failed'
@dataclass
class Record(db.Model):
__tablename__ = 'records'
id: int
status: Status
start: datetime
end: datetime
time_duration:int
camera_id: int
bucket_destination:str
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.Enum(Status), nullable=False, default=Status.scheduled)
start = db.Column(db.DateTime(), nullable=False)
end = db.Column(db.DateTime())
time_duration = db.Column(db.Integer)
camera_id = db.Column(db.Integer, unique=True, nullable=False)
bucket_destination = db.Column(db.String(1000), unique=True, nullable=False)
db.create_all()
执行此代码时出现以下错误:
Traceback (most recent call last): File "src/app.py", line 36, in <module>
app = create_app() File "src/app.py", line 25, in create_app
from routes.record import record_blueprint File "/home/User/VideoCapturer/services/api/src/routes/record/__init__.py",
line 1, in <module>
from .routes import * File "/home/User/VideoCapturer/services/api/src/routes/record/routes.py",
line 4, in <module>
from .models import Record File "/home/User/VideoCapturer/services/api/src/routes/record/models.py",
line 14, in <module>
class Record(db.Model): File "/home/User/VideoCapturer/services/api/src/routes/record/models.py",
line 25, in Record
status = db.Column(db.Enum(Status), nullable=False, default=Status.scheduled) File
"/home/User/.pyenv/versions/3.6.15/lib/python3.6/enum.py", line 326,
in __getattr__
raise AttributeError(name) from None AttributeError: scheduled
知道为什么 SQL 没有正确使用枚举吗?
谢谢
在 Python 中定义枚举的正确语法是:
class Status(Enum):
scheduled = 'scheduled'
你所做的是类型提示。
您好,我有以下型号:
class Status(Enum):
scheduled: 'scheduled'
recording: 'recording'
complete: 'complete'
failed: 'failed'
@dataclass
class Record(db.Model):
__tablename__ = 'records'
id: int
status: Status
start: datetime
end: datetime
time_duration:int
camera_id: int
bucket_destination:str
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.Enum(Status), nullable=False, default=Status.scheduled)
start = db.Column(db.DateTime(), nullable=False)
end = db.Column(db.DateTime())
time_duration = db.Column(db.Integer)
camera_id = db.Column(db.Integer, unique=True, nullable=False)
bucket_destination = db.Column(db.String(1000), unique=True, nullable=False)
db.create_all()
执行此代码时出现以下错误:
Traceback (most recent call last): File "src/app.py", line 36, in <module>
app = create_app() File "src/app.py", line 25, in create_app
from routes.record import record_blueprint File "/home/User/VideoCapturer/services/api/src/routes/record/__init__.py",
line 1, in <module>
from .routes import * File "/home/User/VideoCapturer/services/api/src/routes/record/routes.py",
line 4, in <module>
from .models import Record File "/home/User/VideoCapturer/services/api/src/routes/record/models.py",
line 14, in <module>
class Record(db.Model): File "/home/User/VideoCapturer/services/api/src/routes/record/models.py",
line 25, in Record
status = db.Column(db.Enum(Status), nullable=False, default=Status.scheduled) File
"/home/User/.pyenv/versions/3.6.15/lib/python3.6/enum.py", line 326,
in __getattr__
raise AttributeError(name) from None AttributeError: scheduled
知道为什么 SQL 没有正确使用枚举吗?
谢谢
在 Python 中定义枚举的正确语法是:
class Status(Enum):
scheduled = 'scheduled'
你所做的是类型提示。