【问题标题】:How to resolve this flask sqlalchemy error "sqlalchemy.exc.InvalidRequestError"?如何解决这个烧瓶 sqlalchemy 错误“sqlalchemy.exc.InvalidRequestError”?
【发布时间】:2020-07-03 20:34:12
【问题描述】:
from datetime import datetime
from pack import db,login_manager
from flask_login import UserMixin

# SQLALCHEMY_TRACK_MODIFICATIONS = False

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

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(150), unique = True, nullable = False)
    password = db.Column(db.String(60), nullable = False)
    profile_pic = db.Column(db.String(20), nullable = False, default = "default_profile_pic.jpg")
    posts = db.relationship('Post', backref = 'author', lazy = True)
    comments = db.relationship('Comment', backref = 'commentor', lazy = True)


    def __repr__(self):
        return f"User('{self.username}','{self.email}','{self.profile_pic}')"
    
class Post(db.Model,UserMixin):
    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)
    post_comments = db.relationship('Comment', backref = 'commentor', lazy = 'dynamic')

    def __repr__(self):
        return f"Post('{self.title}','{self.content}','{self.date_posted}','{self.comments}')"

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    comment = db.Column(db.String(100000), nullable = False)
    timestamp = db.Column(db.DateTime, nullable = False, default = datetime.utcnow, index = True)
    user_comment_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)
    post_comment_id = db.Column(db.Integer, db.ForeignKey('post.id'))

    def __repr__(self):
        return f"Post('{self.comment}','{self.user_comment_id}','{self.post_comment_id}')"

这给出了这个错误:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Post->post'. Original exception was: Error creating backref 'commentor' on relationship 'Post.post_comments': property of that name exists on mapper 'mapped class Comment->comment'

请帮我解决这个错误。我想添加一个功能来评论帖子,为此我试图将 Comment 模型与 PostUser 模型连接起来。

【问题讨论】:

    标签: flask sqlalchemy flask-sqlalchemy


    【解决方案1】:

    在您的错误中,您有:Post.post_comments': property of that name exists on mapper 'mapped class Comment->comment

    似乎在User 中有comments,它在评论中创建了一个反向引用'commentor',在Post 中,你有post_comments,它也在评论中创建了一个反向引用'commentor'

    例如Post,你可以把post_comment改成:

    post_comments = db.relationship('Comment', backref = 'post_commented', lazy = 'dynamic')
    

    编辑: 使用 backref 参数定义一对多关系时,不需要user_id(在 Post 中)、user_comment_id(在 Comment 中)、post_comment_id(在 Comment 中):只需删除它们即可。

    你应该再看看https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#one-to-many(backref 部分)

    【讨论】:

    • 即使我更改名称仍然会在 user_comment_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False) post_comment_id = db 中给出错误.Column(db.Integer, db.ForeignKey('post.id')) 两个变量都给出空值
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多