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

时间:2017-06-20 21:43:15

标签: flask peewee flask-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

2 个答案:

答案 0 :(得分:1)

您错误地调用了create_tables:

database.create_tables([Item], True)

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

答案 1 :(得分:-1)

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