G
G
Gleb Radin2022-04-19 23:10:22
PostgreSQL
Gleb Radin, 2022-04-19 23:10:22

Sqlalchemy AmbiguousForeignKeysError?

class User(Base):
    __tablename__ = '_users'
    id = Column(String(), primary_key=True)
    firstname = Column(String(100))
    lastname = Column(String(100))
    reg_date = Column(DateTime(), server_default=text('CURRENT_TIMESTAMP'))
    transactions = relationship('InnerTransaction')

class InnerTransaction(Base):
    __tablename__ = '_transactions'
    id = Column(Integer(), primary_key=True, autoincrement=False)
    sender_id = Column(String(), ForeignKey('_users.id'))
    receiver_id = Column(String(), ForeignKey('_users.id'))
    amount = Column(String())
    created_at = Column(DateTime(), server_default=text('CURRENT_TIMESTAMP'))


These models correctly create tables, but when accessing the tables, an error occurs:

Exception has occurred: AmbiguousForeignKeysError
Could not determine join condition between parent/child tables on relationship User.transactions - 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.


There are similar examples in the documentation, but I didn’t figure out how to apply them in my case.

How to correctly organize the relationship of tables with several foreign keys?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kalapanga, 2022-04-20
@Lorrain

There is an example in the documentation. There is not a similar, but exactly the same case:
https://docs.sqlalchemy.org/en/14/orm/join_conditi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question