On Fri, Mar 29, 2019 at 12:17 PM 'Neil Youngman' via sqlalchemy
<sqlalchemy@googlegroups.com> wrote:
>
> I'm trying to do a select of columns from a join and I simply can't get it to 
> work. I have tried various different ways to specify the join, with and 
> without a select_from() and I can't find a combination that works.
>
> the tables look like
>
> class Dealer(Base):
>     __tablename__ = 'dealers'
>
>     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 = Column(String(64), ForeignKey(u'dealers.id'), 
> nullable=False)
>     servicing_dealer = Column(String(64), ForeignKey(u'dealers.id'), 
> nullable=False)
>
>     def __repr__(self):
>        return "<Vehicle(registration='%s', supplying_dealer='%s', 
> servicing_dealer='%s')>" % (
>            self.registration, self.supplying_dealer, self.servicing_dealer)
>
> and I have tried lots of variations on:
>
> for supplier, phone, registration in   
> session.query(Dealer.name,Dealer.phone_number,Vehicle.registration).select_from(Vehicle).join('supplying_dealer'):
>     print( supplier, phone, registration )
>
> I haven't found a combination that works. Am I missing the obvious?

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)

With those relationships in place, either of these joins should work:

    session.query(Vehicle).join("supplying_dealer")
    session.query(Vehicle).join(Vehicle.supplying_dealer)

Hope that helps,

Simon

-- 
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.

Reply via email to