如何使用 peewee 在模型 FlaskDB 中创建表?

How to create the tables in a model FlaskDB with peewee?

离开文档 here

我有这个代码:

from playhouse.flask_utils import FlaskDB,
app = Flask(__name__)
app.config.from_object(__name__)

flask_db = FlaskDB(app)
database = flask_db.database

class Item(flask_db.Model):
    title = CharField()
    content = TextField()
    category = CharField()

@app.route('/create',methods=('GET','POST'))
def create():
    if request.method == 'POST':
        if request.form.get('title') and request.form.get('content'):
            item = Item.create(
                title = request.form['title'],
                content = request.form['content'],
                category = request.form['category'])
            flash('Item created successfully','success')
            return redirect(url_for('view'),item=item)
        else:
            flash('Title and Content are required.','danger')
    form = ItemForm()
    return render_template('create.html',form=form)

if __name__ == '__main__':
    database.create_tables(Item)
    app.run(debug=True)

他们这样说:

根据应用程序配置数据动态创建 Peewee 数据库实例。

但是,我相信我仍然需要创建表格,事实上,当我在没有执行倒数第二行的情况下尝试创建表格时,我可以看到创建的 blog.db 文件中不存在任何表格。不幸的是,当我现在 运行 时,我得到:

 Traceback (most recent call last):
sqliteext:////Users/conduce-laptop/PycharmProjects/alexmarshall.website/blog.db
  File "/Users/conduce-laptop/PycharmProjects/alexmarshall.website/website2.py", line 73, in <module>
    database.create_tables(Item)
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3855, in create_tables
    create_model_tables(models, fail_silently=safe)
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 5293, in create_model_tables
    for m in sort_models_topologically(models):
  File "playhouse/_speedups.pyx", line 341, in playhouse._speedups.sort_models_topologically (playhouse/_speedups.c:7091)
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 4862, in __iter__
    return iter(self.select())
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3240, in __iter__
    return iter(self.execute())
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3233, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 2912, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3775, in execute_sql
    self.commit()
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3598, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3768, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: no such table: item

我不得不 运行: Item.create_table(fail_silently=True) 在底部而不是 database.create_tables(Item)。显然你只是 运行 它一次(因为你不想重新创建 table

您错误地调用了 create_tables:

database.create_tables([Item], True)

True 允许您多次调用它而不会出错。