'how to point multiple foreign keys to one primary key (python-sqlalchemy)
How can I relate column Users.id to multiple columns in Books table?
Tables :
class Users(base):
__tablename__ = 'users'
id = Column('id', BIGINT, primary_key=True, autoincrement=False)
name = Column('name', VARCHAR(32))
books = relationship("books")
class Books(base):
__tablename_ = 'books'
name = Column('name', VARCHAR(32))
author = Column(BIGINT, ForeignKey("users.id"))
given_by = Column(BIGINT, ForeignKey("users.id"))
read_by = Column(BIGINT, ForeignKey("users.id"))
but when i do that i get this error :
sqlalchemy.exc.AmbiguousForeignKeysError:
Could not determine join condition between parent/child tables on relationship Users.books - 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 tried this and it doesn't work and I'm keep getting the same error:
class Users(base):
__tablename__ = 'users'
id = Column('id', BIGINT, primary_key=True, autoincrement=False)
name = Column('name', VARCHAR(32))
books = relationship("books", foreign_keys=[Books.author, Books.given_by, Books.read_by])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
