flask/sqlalchemy - OperationalError:(sqlite3.OperationalError)没有这样的表

时间:2021-05-01 15:26:44

I get the below error sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: s_amodel [SQL: 'INSERT INTO s_amodel

我得到下面的错误sqlalchemy.exc。OperationalError:(sqlite3.OperationalError)没有这样的表:s_amodel [SQL: '插入到s_amodel中

My files

我的文件

forms.py

from flask_wtf import Form
class SAForm(Form):

....

....

models.py

from app.app_and_db import db 

class SAmodel(db.Model):
    id = db.Column(db.Integer(), primary_key=True)

views.py

from app.app_and_db import app, db
from app.sa.forms import SAForm
from app.sa.models import SAmodel

@sa_blueprint.route('/sa/new', methods=['GET','POST'])
def new_sa():

        form = SAForm(request.form, SAmodel)

        if request.method=='POST' and form.validate():
            model = SAmodel()
            form.populate_obj(model)
            db.session.add(model)
            db.session.commit()
            return redirect(url_for("sa.home_page"))
        return render_template("new_sa.html", form=form)

config.py

SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', 'sqlite:///app.sqlite')

This is what I have. The app.sqlite is created but it is 0 bytes. When I submit something using new_sa() function, I get the above error. Also how is it getting the table name "s_amodel"?

这就是我有的。创建了app.sqlite,但它是0字节。当我使用new_sa()函数提交一些东西时,我得到上面的错误。它如何获得表名“s_amodel”?

Any help would be greatly appreciated. Thanks a lot!

如有任何帮助,我们将不胜感激。谢谢!

1 个解决方案

#1


5  

You're supposed to initialize/create the tables first. Please read the Creating the Database article in the official Flask documentation:

您应该首先初始化/创建表。请阅读官方Flask文档中创建数据库的文章:

Such systems need a schema that tells them how to store that information. So before starting the server for the first time it’s important to create that schema.

这样的系统需要一个模式来告诉它们如何存储这些信息。在第一次启动服务器之前,创建这个模式很重要。

Here's Flask's example of using a schema SQL script to create the database, tables, etc:

下面是Flask使用模式SQL脚本创建数据库、表等的示例:

sqlite3 /tmp/flaskr.db < schema.sql

The recommended way is to use db.create_all() within your app. For example, see: https://github.com/lily-mayfield/staticfuzz/blob/d2e54186f5639a06a5a796f0499a984ca8919ed7/staticfuzz.py#L403

推荐的方法是在应用程序中使用db.create_all()。

#1


5  

You're supposed to initialize/create the tables first. Please read the Creating the Database article in the official Flask documentation:

您应该首先初始化/创建表。请阅读官方Flask文档中创建数据库的文章:

Such systems need a schema that tells them how to store that information. So before starting the server for the first time it’s important to create that schema.

这样的系统需要一个模式来告诉它们如何存储这些信息。在第一次启动服务器之前,创建这个模式很重要。

Here's Flask's example of using a schema SQL script to create the database, tables, etc:

下面是Flask使用模式SQL脚本创建数据库、表等的示例:

sqlite3 /tmp/flaskr.db < schema.sql

The recommended way is to use db.create_all() within your app. For example, see: https://github.com/lily-mayfield/staticfuzz/blob/d2e54186f5639a06a5a796f0499a984ca8919ed7/staticfuzz.py#L403

推荐的方法是在应用程序中使用db.create_all()。