Re: [sqlalchemy] Can't get join() to work

2019-03-29 Thread 'Neil Youngman' via sqlalchemy


On Friday, 29 March 2019 15:30:39 UTC, Neil Youngman wrote:
>
>
> That needs to be:
> supplying_dealer = relationship(Dealer, 
> primaryjoin=supplying_dealer_id == Dealer.dealer_id)
> servicing_dealer = relationship(Dealer, 
> primaryjoin=servicing_dealer_id == Dealer.dealer_id)
>

and now:

>>> for supplier, phone, registration in 
session.query(Dealer.name,Dealer.phone_number,Vehicle.registration).select_from(Vehicle).join(u'supplying_dealer'):
... print( supplier, phone, registration )
... 

returns:

(u'Trumpton Vehicles', u'123-987-4567', u'ABC123D')
(u'Lazytown Vehicles', u'987-123-7654', u'DEF456G')
(u'Lazytown Vehicles', u'987-123-7654', u'GHI789J')

Thanks for the help.

Neil

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


Re: [sqlalchemy] Can't get join() to work

2019-03-29 Thread 'Neil Youngman' via sqlalchemy


On Friday, 29 March 2019 15:10:27 UTC, Neil Youngman wrote:
>
>
> supplying_dealer = relationship(Dealer, supplying_dealer_id == 
> Dealer.dealer_id)
> servicing_dealer = relationship(Dealer, servicing_dealer_id == 
> Dealer.dealer_id)
>
>
That needs to be:
supplying_dealer = relationship(Dealer, primaryjoin=supplying_dealer_id 
== Dealer.dealer_id)
servicing_dealer = relationship(Dealer, primaryjoin=servicing_dealer_id 
== Dealer.dealer_id)

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


Re: [sqlalchemy] Can't get join() to work

2019-03-29 Thread 'Neil Youngman' via sqlalchemy


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 "" % (
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 "" % (
   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.


[sqlalchemy] Can't get join() to work

2019-03-29 Thread 'Neil Youngman' via sqlalchemy
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 "" % (
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 "" % (
   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?

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