如果同一个外键有 2 个字段,如何声明一对多

发布于 2024-09-15 05:09:10 字数 1166 浏览 8 评论 0原文

我是 python(sqlalchemy) 的新手,我正在学习使用 pylons 和 sqlalchemy 构建网站。

当我声明模型之间的关系时,我遇到了问题。我已经尝试了几个小时,但失败了。但我认为这应该是一个基本问题。

我有两个类:User和Article,用户可以创建文章,并修改其他人的文章(如wiki)。 因此,用户创建了文章并编辑了文章。

class Article(Base):
    __tablename__ = 'articles'

    id = Column(Integer, primary_key=True)
    title = ...

    user_id = Column(Integer, ForeignKey('users.id'))
    editor_id = Column(Integer, ForeignKey('users.id'))

    # relations
    user = relationship('User', backref='articles') # -> has error


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String(20))

    def __init__(self):
        pass

但显示错误:

InvalidRequestError: One or more mappers failed to compile. Exception was probably suppressed within a hasattr() call. Message was: Could not determine join condition between parent/child tables on relationship Article.user. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well.

我尝试将 primaryjoin 添加到该行('有错误'),但不知道它应该是什么。我尝试了一些代码,但没有一个有效。

先感谢您!

I'm new to python(sqlalchemy), and I'm learning to build web site with pylons and sqlalchemy.

I have a problem when I declare the relationship between models. I've tried it several hours, but failed. But I think it should be a basic question.

I have two classes: User and Article, user can create articles, and modified the other people's article(like wiki).
So a user has created-articles and edited-articles.

class Article(Base):
    __tablename__ = 'articles'

    id = Column(Integer, primary_key=True)
    title = ...

    user_id = Column(Integer, ForeignKey('users.id'))
    editor_id = Column(Integer, ForeignKey('users.id'))

    # relations
    user = relationship('User', backref='articles') # -> has error


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String(20))

    def __init__(self):
        pass

But there is an error displayed:

InvalidRequestError: One or more mappers failed to compile. Exception was probably suppressed within a hasattr() call. Message was: Could not determine join condition between parent/child tables on relationship Article.user. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well.

I tried to add primaryjoin to the line('has error'), but don't know what it should be. I tried some codes, but none works.

Thank you in advance!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-09-22 05:09:10

啊,那是显而易见的。

Article 类有两个对 User 的引用,user_id 和 editor_id,因此 SQLA 不知道对您的关系使用其中哪一个。只需使用显式的primaryjoin:

user = relation('User', backref='articles', primaryjoin="Article.user_id==User.id")

Ah, thats obvious one.

Article class has two references to User, user_id and editor_id, so SQLA does not know which one of them to use for your relation. Just use explicit primaryjoin:

user = relation('User', backref='articles', primaryjoin="Article.user_id==User.id")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文