On Friday, 29 March 2019 13:48:33 UTC, Simon King wrote: > > > You haven't created a relationship() between Vehicle and Dealer, which > reduces your options a little bit. This should work: > > session.query(Vehicle).join(Dealer, Vehicle.supplying_dealer == > Dealer.id) > > ie. you need to be explicit about the class you are joining to, and > the join condition. The join condition is necessary because you have 2 > foreign keys between Vehicle and Dealer. > > You might want to consider creating relationships between the two > classes, something like this: > > class Vehicle(Base): > __tablename__ = 'vehicles' > > registration = Column(String(10), primary_key=True, nullable=False) > # note that I've renamed these columns > supplying_dealer_id = Column(String(64), > ForeignKey(u'dealers.id'), nullable=False) > servicing_dealer_id = Column(String(64), > ForeignKey(u'dealers.id'), nullable=False) > > supplying_dealer = relationship(Dealer, supplying_dealer_id == > Dealer.id) > servicing_dealer = relationship(Dealer, servicing_dealer_id == > Dealer.id) >
I'd missed the need to set a relationship in addition to the foreign key. I've done that and changed Dealer.id to Dealer.dealer_id. I can't now load any data. It says "sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Vehicle.supplying_dealer - there are no foreign keys linking these tables via secondary table 'vehicles.supplying_dealer_id = dealers.dealer_id'. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions." It looks OK to me. The current table definitions are: class Dealer(Base): __tablename__ = 'dealers' dealer_id = Column(String(64), primary_key=True, nullable=False) name = Column(String(100), nullable=False) phone_number = Column(String(64), nullable=False) def __repr__(self): return "<Dealer(supplying_dealer='%s', registration='%s', servicing_dealer='%s')>" % ( self.supplying_dealer, self.registration, self.servicing_dealer) class Vehicle(Base): __tablename__ = 'vehicles' registration = Column(String(10), primary_key=True, nullable=False) supplying_dealer_id = Column(String(64), ForeignKey(u'dealers.dealer_id'), nullable=False) servicing_dealer_id = Column(String(64), ForeignKey(u'dealers.dealer_id'), nullable=False) supplying_dealer = relationship(Dealer, supplying_dealer_id == Dealer.dealer_id) servicing_dealer = relationship(Dealer, servicing_dealer_id == Dealer.dealer_id) def __repr__(self): return "<Vehicle(registration='%s', supplying_dealer='%s', servicing_dealer='%s')>" % ( self.registration, self.supplying_dealer, self.servicing_dealer) -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.