简体   繁体   English

peewee.OperationalError:表要点没有名为name的列

[英]peewee.OperationalError: table gist has no column named name

I keep getting this error in my code when it seems that there is no problem with it. 当似乎没有问题时,我会在代码中不断收到此错误。 I am using Peewee and SQLite with Flask. 我在Flask中使用Peewee和SQLite。

Here is my code. 这是我的代码。

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()

Here is the code that creates the instance: 这是创建实例的代码:

@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)

Thanks for any help that you can give! 感谢您提供的任何帮助!

EDIT: Stacktrace for those interested 编辑:那些有兴趣的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

I got the same error and fixed it by deleting the database .db file (in your case "gist.db"). 我遇到了相同的错误,并通过删除数据库.db文件(在您的情况下为“ gist.db”)进行了修复。

If after the database file was created, you modified or added any model fields, then peewee doesn't recognize the modified and/or new fields in the original database and throws this error. 如果在创建数据库文件之后,您修改或添加了任何模型字段,则peewee无法识别原始数据库中的已修改和/或新字段,并引发此错误。 After deleting the old .db file, a new one with the most recent fields will be recreated upon start-up and that resolved the issue for me. 删除旧的.db文件后,将在启动时重新创建一个具有最新字段的文件,该文件为我解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM