python - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 即使在使用 db.create_all() 之后也没有这样的表
问题描述
当我将数据输入数据库时,出现操作错误 no such table。我尝试过使用db.create_all()
,但它不起作用。这是错误的图像。
我已经db.create_all()
在应用程序中尝试过,也使用过终端,但它不起作用。数据库已创建,但未在其中创建表。
模型.py
from flask import app
from flask_sqlalchemy import SQLAlchemy
from application import app
from application import db
class Employee(db.Model):
id = db.Column(db.Integer(),primary_key=True)
username = db.Column(db.String(length=100),nullable=False)
email_address = db.Column(db.String(length=50),nullable=False)
password_hash = db.Column(db.String(length=60),nullable=False
初始化.py
from os import name
from flask import Flask, current_app, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
with app.app_context():
print(current_app.name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'this is secret key'
db = SQLAlchemy(app)
路线.py
from application import routes
import re
from flask.helpers import url_for
from wtforms.form import Form
from application import app
from flask import Flask, render_template,redirect,url_for,flash,request
from application.forms import EmployeeRegistration, LoginForm
from application.models import Employee
from application import db
from werkzeug.security import generate_password_hash,check_password_hash
@app.route('/')
def index():
return render_template('index.html')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/register',methods=['GET','POST'])
def register():
form = EmployeeRegistration()
if form.validate_on_submit():
employee_to_create = Employee(
username=form.username.data,
email_address = form.email_address.data,
password_hash= generate_password_hash(form.password1.data)
)
db.session.add(employee_to_create)
db.session.commit()
return redirect(url_for('home'))
if form.errors != {}:# if there are not errors from the validations
for err_msg in form.errors.values():
flash(f"There was an error : {err_msg}",category='danger')
return render_template('register.html',form=form)
@app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
return render_template('login.html',form=form)
解决方案
推荐阅读
- c++ - 为什么默认的复制构造函数会忽略派生类的虚方法覆盖?
- angular - Angular:HttpRequest.clone() - params 和 setParams 有什么区别?
- java - 如何在没有 JSP 页面的情况下将参数发送到 Servlet
- ionic4 - 如何将数据从 *ngFor 项传递到单击方法
- c# - 在 Elasticsearch 的查询 DSL 中使用“MatchPhrasePrefix”
- sql - 如何找到产品未知数量选项的所有组合?
- architecture - 微软是否应用了企业架构?如果是/否,为什么?
- php - 如何在 PHP 中获取高级搜索的结果
- javascript - 构造对象时如何使用三元运算符?
- python - 带有错误标题分隔符的 Pandas read_csv