flask/sqlalchemy-OperationalError:(sqlite3.OperationalError)没有这样的表 [英] flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table
问题描述
我收到以下错误:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: s_amodel [SQL: 'INSERT INTO s_amodel
我的文件forms.py
从flask_wtf导入表中的
My files forms.py
from flask_wtf import Form
class SAForm(Form):
models.py
来自app.app_and_db的
models.py
from app.app_and_db import db
class SAmodel(db.Model):
id = db.Column(db.Integer(), primary_key=True)
views.py
来自app.app_and_db导入应用程序的
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')
这就是我所拥有的.app.sqlite已创建,但为0字节.当我使用new_sa()函数提交内容时,出现上述错误.还有如何获得表名"s_amodel"?
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"?
任何帮助将不胜感激.非常感谢!
Any help would be greatly appreciated. Thanks a lot!
推荐答案
您应该首先初始化/创建表.请阅读Flask官方文档中的 创建数据库一文.:
You're supposed to initialize/create the tables first. Please read the Creating the Database article in the official Flask documentation:
此类系统需要一个架构来告诉他们如何存储该信息.因此,在首次启动服务器之前,创建该架构很重要.
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.
这是Flask使用模式SQL脚本创建数据库,表等的示例:
Here's Flask's example of using a schema SQL script to create the database, tables, etc:
sqlite3 /tmp/flaskr.db < schema.sql
推荐的方法是在应用程序中使用 db.create_all()
.例如,请参阅: https://github.com/lily-mayfield/staticfuzz/blob/d2e54186f5639a06a5a796f0499a984ca8919ed7/staticfuzz.py#L403
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
这篇关于flask/sqlalchemy-OperationalError:(sqlite3.OperationalError)没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!