简体   繁体   中英

SqlAlchemy Error on Creating multiple foreign key to one table

I am new using sqlAlchemy and having problem creating new tables, specially when it comes around 2 foreign keys pointing to 1 table:

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

The error I am getting is:

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

I have try different things but I get same result. What am I doing wrong? Thanks In advance.

I know this is an old question, but I had the same problem. I hope to help others with the answer.

This issue is addressed in the sqlalchemy documentation.

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

The way you wrote the code, sqlalchemy can't really understand which relationship to choose, because you have 2 of the same relationship. So you have to describe to sqlalchemy that there are 2 relationships to the same table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM