当我的代码看起来没有问题时,我总是在我的代码中收到这个错误。我在 Flask 中使用 Peewee 和 SQLite。
这是我的代码。
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)
感谢您提供的任何帮助!
编辑:感兴趣的人的堆栈跟踪
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
我遇到了同样的错误并通过删除数据库 .db 文件(在您的情况下为“gist.db”)来修复它。
如果在创建数据库文件后,您修改或添加了任何模型字段,那么 peewee 无法识别原始数据库中修改的和/或新的字段并抛出此错误。删除旧的 .db 文件后,将在启动时重新创建一个包含最新字段的新文件,这为我解决了这个问题。
正如@davejlin 所写,如果删除 db 文件并重新创建它,问题将得到解决。 或者,如果数据库中有重要数据,则可以使用Schema Migrations。
来自peewee文档:
创建表后,如果您选择修改数据库模式(通过添加、删除或以其他方式更改列),您将需要:
- 删除表格并重新创建它。
- 运行一个或多个 ALTER TABLE 查询。 Peewee 附带了一个模式迁移工具,可以大大简化这一过程。查看 schema migrations 文档以获取详细信息。