python - Flask + SQLAlchemy:为表创建索引列
问题描述
我在使用 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)
应用程序/任务/test.py
@periodic_task(run_every=crontab(minute="*"), bind=True)
def periodic_run_get_manifest(self):
with app.app_context():
task_id = self.request.id
s = Schedule()
s.taskId = task_id
db.session.add(s)
db.session.commit()
rowId = db.session.query(Schedule).order_by(Schedule.scheduleId.desc()).first()
当定期任务运行时,我收到以下错误:
AttributeError:类型对象“Schedule”没有属性“scheduleId”
解决方案
推荐阅读
- ffmpeg - ffmpeg 如何将标头信息添加到 pcm 中?
- azure - Azure 架构实施思路
- admob - 如何在 google admob 中链接/更改已链接的应用程序?
- git - 如何在 Eclipse 中克隆 git 私有存储库?
- html - 我需要删除这个 wp-block-library-css (Gutenberg CSS) 吗?
- python - 详细视图 Django 上的 FormMixin
- kubernetes - 在 PV/PVC 中引用存储类之前,是否需要在 Kubernetes 中创建存储类?
- crystal-reports - 如何在 Crystal Report 的详细信息部分中仅创建一个列标题
- python - 使用 time.sleep 时 Python 多处理不起作用
- android - 如何在域层中获取字符串资源