Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果同一外键有两个字段,如何声明一对多_Python_Sqlalchemy_Pylons_One To Many - Fatal编程技术网

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

Python 如果同一外键有两个字段,如何声明一对多,python,sqlalchemy,pylons,one-to-many,Python,Sqlalchemy,Pylons,One To Many,我不熟悉python(sqlalchemy),我正在学习使用挂架和sqlalchemy构建网站 我在声明模型之间的关系时遇到问题。我试了好几个小时,但失败了。但我认为这应该是一个基本问题 我有两个类:User和Article,User可以创建文章,修改其他人的文章(比如wiki)。 因此,用户创建了文章并编辑了文章 class Article(Base): __tablename__ = 'articles' id = Column(Integer, primary_key=T

我不熟悉python(sqlalchemy),我正在学习使用挂架和sqlalchemy构建网站

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

我有两个类:User和Article,User可以创建文章,修改其他人的文章(比如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
添加到行('has error'),但不知道应该是什么。我试过一些代码,但都不管用


提前谢谢你

啊,这是显而易见的

Article类有两个对User的引用,User\u id和editor\u id,所以SQLA不知道将它们中的哪一个用于您的关系。只需使用显式primaryjoin:

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

您还可以提供您的用户类吗?或者它在代码中也是虚拟的?用户类被更新了,我删除了一些未使用的字段。用户中没有关系声明,只有一些列。谢谢你,丹尼尔