sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:user

时间:2019-07-22 22:34:27

标签: python sqlite flask sqlalchemy

我一直收到此错误 sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:用户,但是我不知道为什么,因为我是通过sqlite3命令在好目录下创建数据库的database.db和.tables。我尝试了db.create_all(),但如果它在db声明之前,则当然不起作用,但是如果我将它放在它之后,则不会更改任何内容

我还有另一个问题,那就是db。“”始终被标记为错误。我试图安装pylint-flask并将其加载到设置中。它工作了大约5分钟,然后再次返回错误(“ SQLAlchemy”的实例没有“ Column”成员pylint(无成员)

from flask import Flask, render_template, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm 
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, Length
from flask_sqlalchemy  import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user


app = Flask(__name__)
app.config['SECRET_KEY'] = 'Thisissupposedtobesecret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sqlite-tools-win32-x86-3290000/database.db'
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(15), unique=True)
    email = db.Column(db.String(50), unique=True)
    password = db.Column(db.String(80))

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class LoginForm(FlaskForm):
    username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])
    remember = BooleanField('remember me')

class RegisterForm(FlaskForm):
    email = StringField('email', validators=[InputRequired(), Email(message='Invalid email'), Length(max=50)])
    username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])

1 个答案:

答案 0 :(得分:0)

定义模型后,您需要调用create_all()commit()来更新对数据库的更改。

例如。

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(15), unique=True)
    email = db.Column(db.String(50), unique=True)
    password = db.Column(db.String(80))

db.create_all()
db.session.commit()