在一个表上创建多个外键时发生SqlAlchemy错误

时间:2018-12-23 01:44:49

标签: python sqlalchemy flask-sqlalchemy

我是使用sqlAlchemy的新手,在创建新表时遇到问题,特别是当它涉及指向1个表的2个外键时:

class Offers(db.Model):
    __tablename__ = 'offers'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    contact_ign = db.Column(db.String(100))
    conversion_rate = db.Column(db.Float)
    stock = db.Column(db.Integer)
    create_date = db.Column(db.DateTime(timezone=True), default=func.now())
    currency_pair = db.relationship('CurrencyPairs', backref='pair', lazy='dynamic')

class CurrencyPairs(db.Model):
    __tablename__ = 'currency_pairs'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    league = db.Column(db.String(100))
    pair_id = db.Column(db.Integer, db.ForeignKey('offers.id'))
    want = db.relationship('Currency', backref='want', lazy='dynamic')
    have = db.relationship('Currency', backref='have', lazy='dynamic')

class Currency(db.Model):
    __tablename__ = 'currency'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False)
    poe_trade = db.Column(db.Integer, nullable=False)
    poe_official = db.Column(db.String(10), nullable=False)
    tier = db.Column(db.Integer, nullable=False)
    want_id = db.Column(db.Integer, db.ForeignKey('currency_pairs.id'))
    have_id = db.Column(db.Integer, db.ForeignKey('currency_pairs.id'))

我得到的错误是:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|CurrencyPairs|currency_pairs'. Original exception was: Could not determine join condition b
etween parent/child tables on relationship CurrencyPairs.want - 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 refe
rence to the parent table

我尝试了不同的方法,但是得到了相同的结果。 我究竟做错了什么? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

我知道这是一个老问题,但是我有同样的问题。我希望可以帮助其他人。

此问题已在sqlalchemy文档中解决。

https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#handling-multiple-join-paths

class Offers(db.Model):
    __tablename__ = 'offers'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    contact_ign = db.Column(db.String(100))
    conversion_rate = db.Column(db.Float)
    stock = db.Column(db.Integer)
    create_date = db.Column(db.DateTime(timezone=True), default=func.now())
    currency_pair = db.relationship('CurrencyPairs', backref='pair', lazy='dynamic')

class CurrencyPairs(db.Model):
    __tablename__ = 'currency_pairs'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    league = db.Column(db.String(100))
    pair_id = db.Column(db.Integer, db.ForeignKey('offers.id'))

    want_currency = relationship("Currency", foreign_keys='[Currency.want_id]', back_populates="want_currency_pairs")
    have_currency = relationship("Currency", foreign_keys='[Currency.have_id]', back_populates="have_currency_pairs")

class Currency(db.Model):
    __tablename__ = 'currency'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False)
    poe_trade = db.Column(db.Integer, nullable=False)
    poe_official = db.Column(db.String(10), nullable=False)
    tier = db.Column(db.Integer, nullable=False)

    want_currency_pairs = relationship(CurrencyPairs, foreign_keys="[Currency.want_id]", back_populates="want_currency")
    have_currency_pairs = relationship(CurrencyPairs, foreign_keys="[Currency.have_id]", back_populates="have_currency")

您编写代码的方式,sqlalchemy不能真正理解要选择哪个关系,因为您有两个相同的关系。因此,您必须向sqlalchemy描述同一表有2种关系。