2

I have the following code for my DB models:

# account_manager.py class AccountManager(User): __tablename__ = 'account_managers' id = Column(UUID(as_uuid=True), ForeignKey('users.id'), primary_key=True) project_id = Column(Integer()) collaborators = relationship("CollaboratorUser", back_populates='manager') __mapper_args__ = { 'polymorphic_identity': 'account_manager' } # collaborator_user.py class CollaboratorUser(User): __tablename__ = 'collaborators' id = Column(UUID(as_uuid=True), ForeignKey('users.id'), primary_key=True) manager_id = Column(UUID(as_uuid=True), ForeignKey('account_managers.id')) manager = relationship("AccountManager", back_populates='collaborators', foreign_keys=[manager_id]) __mapper_args__ = { 'polymorphic_identity': 'collaborator' } # user.py class User(Base): __tablename__ = 'users' id = id_col() email = Column(String) full_name = Column(String) temporary = Column(Boolean) type = Column(String) __mapper_args__ = { 'polymorphic_on': type } 

When I attempt to load a model, I get the following error:

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

I looked up this error and ever solution I saw had to do with adding the foreign_keys parameter to the relationship function.

Any help would be appreciated.

Thanks!

1 Answer 1

4

Figured it out. Changed

manager = relationship("AccountManager", back_populates='collaborators', foreign_keys=[manager_id]) 

to

manager = relationship("AccountManager", backref='collaborators', foreign_keys=[manager_id]) 

and removed collaborators from AccountManager. This will automatically add the colloaborators field to AccountManager. This seems at odds with the documentation, but there you go.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.