我的代码中出现此错误时,似乎没有问题。我正在使用Peewee和SQLite与Flask。
这是我的代码。
from peewee import *
import datetime
DB = SqliteDatabase("gist.db")
class Gist(Model):
name = CharField()
content = TextField()
description = TextField()
join_date = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DB
def initialize():
DB.connect()
DB.create_tables([Gist], safe=True)
DB.close()
以下是创建实例的代码:
@app.route("/", methods=("GET", "POST"))
def index():
form = forms.GistForm()
if form.validate_on_submit():
models.Gist.create(name=form.name.data,
content=form.content.data,
description=form.description.data)
flash("Message posted! Thanks!", "success")
return render_template("index.html", form=form)
感谢您提供任何帮助!
编辑:感兴趣的人的Stacktrace
Traceback (most recent call last):
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1403, in handle_exc
reraise(exc_type, exc_value, tb)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1477, in full_dispa
rv = self.handle_user_exception(e)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1381, in handle_use
reraise(exc_type, exc_value, tb)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1475, in full_dispa
rv = self.dispatch_request()
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1461, in dispatch_r
return self.view_functions[rule.endpoint](**req.view_args)
File "B:\Sites\GistClone\app.py", line 21, in index
description=form.description.data)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 4494, in create
inst.save(force_insert=True)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 4680, in save
pk_from_cursor = self.insert(**field_dict).execute()
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 3213, in execute
cursor = self._execute()
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 2628, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 3461, in execute_sql
self.commit()
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 3285, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 127, in reraise
raise value.with_traceback(tb)
File "C:\Users\Max\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 3454, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: table gist has no column named name
答案 0 :(得分:0)
我得到了同样的错误并通过删除数据库.db文件(在你的情况下" gist.db")修复了它。
如果在创建数据库文件后修改或添加了任何模型字段,则peewee不会识别原始数据库中的已修改和/或新字段并抛出此错误。删除旧的.db文件后,将在启动时重新创建具有最新字段的新文件,并为我解决了该问题。