sqlalchemy.exc.InvalidRequestError:触发映射器:“映射的类User-> user”。原始异常是:未映射类'flaskblog.Post'

时间:2020-05-03 05:59:02

标签: python flask-sqlalchemy

当我尝试使用python解释器从cmd测试数据库时,发生了此错误。创建表时没有出现错误,但似乎仍然存在问题。

模型是这样的:

    class User(db.Model):
    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)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable = False)
    date_posted = db.Column(db.DateTime, nullable = False, default = datetime.utcnow)
    content = db.Column(db.Text, nullable = False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)

0 个答案:

没有答案