[英]sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
i have been getting this error sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user but i dont know why since i created my database at the good directory by the command sqlite3 database.db and .tables .I tried db.create_all() but if it is before the declaration of db it of course not work but if i put it after it does not change anything我一直收到这个错误sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user但我不知道为什么,因为我通过命令 sqlite3 database.db 和 .tables 在好目录中创建了我的数据库。我试过 db .create_all() 但如果它在 db 的声明之前它当然不起作用但如果我把它放在它之后不会改变任何东西
I have an other problem which is that db."" is always marked as an error.我还有一个问题,那就是 db."" 总是被标记为错误。 I tried to install pylint-flask and loading it in the settings.
我尝试安装 pylint-flask 并在设置中加载它。 It worked for around 5 mins then the errors are back again ( Instance of 'SQLAlchemy' has no 'Column' memberpylint(no-member) )
它工作了大约 5 分钟,然后错误又回来了( 'SQLAlchemy' 的实例没有 'Column' memberpylint(no-member) )
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)])
after defining your Model you need to call create_all()
and commit()
to update the changes to the database.定义模型后,您需要调用
create_all()
和commit()
来更新对数据库的更改。
for example..例如..
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.