如何引用外键 table 两次?

How do I refer to a foreign key table twice?

我收到以下错误:

u'detail': u"One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: Could not determine join condition between parent/child tables on relationship Vote.user - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table."

Table A 被定义为:

class User(postgres.Model):

    def __init__(self,
        name
    ):
        self.name = name

    id = postgres.Column(postgres.Integer , primary_key=True , autoincrement=True)
    name = postgres.Column(postgres.String(32) , nullable=False , unique=True)

Table B 被定义为:

class Vote(postgres.Model):

    def __init__(self,
        user_id,
        responder_id,
        #timestamp_request,
        #timestamp_respond,
        value 
    ):
        self.user_id = user_id
        self.responder_id = responder_id
        #self.timestamp_request = timestamp_request
        #self.timestamp_respond = timestamp_respond
        self.value = value

    id = postgres.Column(postgres.Integer , primary_key=True , autoincrement=True)
    user_id = postgres.Column(postgres.Integer , postgres.ForeignKey('user.id'))
    user = postgres.relationship(User , backref=postgres.backref('votes_user'))
    responder_id = postgres.Column(postgres.Integer , postgres.ForeignKey('user.id'))
    responder = postgres.relationship(User , backref=postgres.backref('votes_responder'))
    timestamp_request = postgres.Column(postgres.DateTime , default=datetime.datetime.utcnow , nullable=False , unique=False)
    timestamp_respond = postgres.Column(postgres.DateTime , default=datetime.datetime.utcnow , onupdate=datetime.datetime.utcnow , nullable=False , unique=False)
    value = postgres.Column(postgres.Enum('up' , 'down' , name='vote_value_enum') , nullable=True)

SQLAlchemy 无法发现关系路径。

user_id = Column(ForeignKey('user.id'))
user = relationship(User, backref=backref('votes_user'))
responder_id = Column(ForeignKey('user.id'))
responder = relationship(User, backref=backref('votes_responder'))

responder 关系是否必须使用 responder_iduser_id 加入?我知道这对我们来说很明显,但是 SQLAlchemy 在这里不考虑列名。您可以将 responder_id 重命名为 foobar,这不会有任何区别。

定义要用于每个关系的外键。

user = relationship(User, foreign_keys=[user_id], backref=backref('votes_user'))
responder = relationship(User, foreign_keys=[responder_id], backref=backref('votes_responder'))