Why do I get the TraceBack
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition
between parent/child tables on relationship County.Legislators -
there are no foreign keys linking these tables.
Ensure that referencing columns are associated with a
ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
with the following models:
class County(Base):
__tablename__ = 'tblCounty'
CountyCode = Column('CountyCode', String, primary_key=True)
Legislators = relationship('Legislators', backref='County', lazy='dynamic')
class Legislators(Base):
__tablename__ = 'VLegislators'
EmployeeNo = Column('EmployeeNo', String, primary_key=True)
CountyCode = Column('CountyCode', String, ForeignKey('County.CountyCode'))
I'm trying to map a public facing MS SQL database provided by the State of New Hampshire. So no schema changes allowed.
Why does it complain about the lack of a ForeignKey relation when one is clearly defined in class Legislators?
Why do I get the TraceBack sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship County.Legislators - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
Exceptions used with SQLAlchemy. The base exception class is SQLAlchemyError. Exceptions which are raised as a result of DBAPI exceptions are all subclasses of DBAPIError. exceptionsqlalchemy.exc.AmbiguousForeignKeysError(*arg, **kw)¶
ProgrammingError is a class within the sqlalchemy.exc module of the SQLAlchemy project. ArgumentError , DataError , DatabaseError , IntegrityError , InvalidRequestError , NoInspectionAvailable , NoSuchTableError , OperationalError , and UnsupportedCompilationError are several other callables with code examples from the same sqlalchemy.exc package.
Where above, SQLAlchemy can’t know automatically which columns should connect to which for the right_nodes and left_nodes relationships. The relationship.primaryjoin and relationship.secondaryjoin arguments establish how we’d like to join to the association table.
AFAIK you should use tablename in ForeignKey:
CountyCode = Column('CountyCode', String, ForeignKey('tblCounty.CountyCode'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With