运行代码时,出现此错误:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: user.about
[SQL: SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.image_file AS user_image_file, user.password AS user_password, user.root AS user_root, user.about AS user_about
FROM user
WHERE user.id = ?]
[parameters: (6,)]
此表确实存在并且设置了默认值,如何阻止这种情况发生?
编辑:我的models.py: 这是我所拥有的模型python文件,删去了一些无关的位。
from website import db, login_manager
from flask_login import UserMixin
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
followers = db.Table('followers',
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
root = db.Column(db.Boolean, nullable=False, default=False)
about = db.Column(db.String(60), nullable=True, default="Welcome to my page!")
编辑2:如果我尝试User.__table__.columns.keys()
,则打印出来:
像['id', 'username', 'email', 'image_file', 'password', 'root', 'about']
一样,但仍然无法识别user.about吗?
答案 0 :(得分:0)
我从reddit(russon77)上的一个人那里找到了答案,基本上就是下载SQLBrowser并从那里编辑并创建相同的列并且可以工作,不需要太多的迁移或任何其他事情!