postgresql - 如何两次引用外键表?

标签 postgresql flask sqlalchemy flask-restless

我收到以下错误:

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."

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)

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'))

关于postgresql - 如何两次引用外键表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959083/

相关文章:

python - sqlalchemy 的 xml 配置

python - 在 SQLAlchemy 中获取按相同字段值分组的结果

python - flask - 何时关闭 mysql 连接?

python - 创建后出现未知数据库错误(sqlalchemy)

node.js - 在 Node.js Sequelize 中对连接表执行关联

postgresql - 有什么办法可以加快这个 Postgres 位图堆扫描的速度吗?

python - 使用curl在不同操作系统中调用Flask REST服务方法

python - SQLAlchemy 更新插入

sql - PostgreSql:用于创建具有 1000 列的表的脚本

sql - select from ... - 基于 JSON 格式的值