Flask+SQLAlchemy:为tab创建索引列

2024-09-29 01:29:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难用SQLALchemy为sqlite3数据库中的表创建索引。每次通过celery的定期任务将一行添加到数据库中,该行都将被索引。在

也许我的理解有缺陷。。我认为一旦第一行数据到达数据库,db.Index('scheduleId', Schedule.taskId)会自动创建一个值为1的新的“scheduleId”列?在

应用程序/模型.py

class Schedule(db.Model):
    __tablename__ = 'schedule'
    taskId = db.Column(db.String(50), primary_key=True)

    def __init__(self, **kwargs):
        super(Schedule, self).__init__(**kwargs)

db.Index('scheduleId', Schedule.taskId)

应用程序/任务/测试.py

^{pr2}$

当定期任务运行时,我得到以下错误:

AttributeError: type object 'Schedule' has no attribute 'scheduleId'


Tags: 数据pyself数据库应用程序dbindexsqlalchemy
1条回答
网友
1楼 · 发布于 2024-09-29 01:29:25

修改类模型

class Schedule(db.Model):
        __tablename__ = 'schedule'
        taskId = db.Column(db.String(50), unique=True)
        scheduleId = db.Column(db.Integer, primary_key=True) # will create index and autoincrement both

Autoincrement Docs

Index Docs

相关问题 更多 >