在多对多中使用关联代理时是否需要反向引用?

发布于 2024-12-11 04:52:58 字数 2983 浏览 0 评论 0原文

因此,使用 SqlAlchemy,我在用户模型和评论模型之间创建了一个相当简单的多对多关系。

users.py

class UsersModel(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String(50))
    email = Column(String(50))
    issues = relationship(IssuesModel, backref="user")
    comments = relationship(IssueCommentsModel, backref="user")
    voted_comments = association_proxy('users_comments', 'comment')

    def __init__(self, **fields):
        self.__dict__.update(fields)

    def __repr__(self):
        return "<Users('%s', '%s', '%s')>" % (self.id,
                                              self.username,
                                              self.email)

users_comments.py

class UsersCommentsModel(Base):
    __tablename__ = 'users_comments'
    user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
    comment_id = Column(Integer, ForeignKey('issue_comments.id'), primary_key=True)
    vote = Column(Integer(1))

    user = relationship(UsersModel,
                        backref="users_comments")
    comment = relationship(IssueCommentsModel,
                           backref="users_comments")

    def __init__(self, **fields):
        self.__dict__.update(fields)

    def __repr__(self):
        return "<UsersComments('%s', '%s', '%s')>" % (self.user_id,
                                                      self.comment_id,
                                                      self.vote)    

issue_comments.py

class IssueCommentsModel(Base):
    __tablename__ = 'issue_comments'

    id = Column(Integer, primary_key=True)
    body = Column(String(300))
    created = Column(DateTime)
    change_time = Column(DateTime)
    score = Column(Integer(100), default=0)
    issue_id = Column(Integer, ForeignKey('issues.id'))
    user_id = Column(Integer, ForeignKey('users.id'))
    voted_users = association_proxy('users_comments', 'user')

    def __init__(self, **fields):
        self.__dict__.update(fields)

    def __repr__(self):
        return "<IssueComments('%s', '%s', '%s', '%s', '%s', '%s')>" % (self.id, 
                                                                        self.body, 
                                                                        self.issue, 
                                                                        self.user, 
                                                                        self.created, 
                                                                        self.change_time)

每个用户都可以创建评论,也可以对评论进行赞成/反对投票。上面的代码是完整的工作代码。我的问题是这样的。当我删除 UsersCommentsModel 类中的两个 backref 时,代码不再起作用并引发 InvalidRequestError 。它指出在 UsersModel 映射器上找不到 users_comments 属性。我认为这很奇怪,b/c 我认为您将能够通过中央 users_comments 关系模型代理所有关系,并且永远不必在用户/评论模型中实际存储该模型的实例。

问题:

有没有办法删除 backrefs,这样用户/评论模型中就不会存储其他属性?

So using SqlAlchemy, I'm creating a fairly simple many-to-many relationship between a users model and a comments model.

users.py

class UsersModel(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String(50))
    email = Column(String(50))
    issues = relationship(IssuesModel, backref="user")
    comments = relationship(IssueCommentsModel, backref="user")
    voted_comments = association_proxy('users_comments', 'comment')

    def __init__(self, **fields):
        self.__dict__.update(fields)

    def __repr__(self):
        return "<Users('%s', '%s', '%s')>" % (self.id,
                                              self.username,
                                              self.email)

users_comments.py

class UsersCommentsModel(Base):
    __tablename__ = 'users_comments'
    user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
    comment_id = Column(Integer, ForeignKey('issue_comments.id'), primary_key=True)
    vote = Column(Integer(1))

    user = relationship(UsersModel,
                        backref="users_comments")
    comment = relationship(IssueCommentsModel,
                           backref="users_comments")

    def __init__(self, **fields):
        self.__dict__.update(fields)

    def __repr__(self):
        return "<UsersComments('%s', '%s', '%s')>" % (self.user_id,
                                                      self.comment_id,
                                                      self.vote)    

issue_comments.py

class IssueCommentsModel(Base):
    __tablename__ = 'issue_comments'

    id = Column(Integer, primary_key=True)
    body = Column(String(300))
    created = Column(DateTime)
    change_time = Column(DateTime)
    score = Column(Integer(100), default=0)
    issue_id = Column(Integer, ForeignKey('issues.id'))
    user_id = Column(Integer, ForeignKey('users.id'))
    voted_users = association_proxy('users_comments', 'user')

    def __init__(self, **fields):
        self.__dict__.update(fields)

    def __repr__(self):
        return "<IssueComments('%s', '%s', '%s', '%s', '%s', '%s')>" % (self.id, 
                                                                        self.body, 
                                                                        self.issue, 
                                                                        self.user, 
                                                                        self.created, 
                                                                        self.change_time)

Each user has the ability to both create comments, and either up/down vote them. The above code is completely working code. My question is this. When I remove the two backrefs in the UsersCommentsModel class, the code no longer works and an InvalidRequestError is thrown. It states that the users_comments attribute cannot be found on the UsersModel mapper. I thought this was strange, b/c I would think you would be able to proxy all relationships through the central users_comments relationship model, and never have to actually store an instance of that model in the users/comments models.

Question:

Is there any way to remove the backrefs, so no other attributes are stored in the users/comments models?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文