首页 > 解决方案 > 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)

标签: pythonflasksqlalchemy

解决方案


推荐阅读