0

离开这里的文档。

我有这个代码:

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
4

2 回答 2

1

您错误地调用了 create_tables:

database.create_tables([Item], True)

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

于 2017-06-21T16:20:41.430 回答
-1

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

于 2017-06-21T01:58:06.610 回答