Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correct this sqlalchemy.exc.NoForeignKeysError?

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?

like image 602
Dan Garthwaite Avatar asked Jan 18 '15 22:01

Dan Garthwaite


People also ask

Why do I get the traceback SQLAlchemy noforeignkeyserror?

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.

What are the exceptions used with SQLAlchemy?

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

What is programmingerror in SQL alchemy?

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.

How does SQLAlchemy know which columns to connect to which?

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.


1 Answers

AFAIK you should use tablename in ForeignKey:

CountyCode = Column('CountyCode', String, ForeignKey('tblCounty.CountyCode'))
like image 198
bav Avatar answered Oct 02 '22 14:10

bav