On Jun 1, 3:34 pm, "Michael Bayer" <[email protected]> wrote:
> George Sakkis wrote:
>
> > I'm trying to use the primaryjoin/foreign_keys parameters (http://
> >www.sqlalchemy.org/docs/05/mappers.html#specifying-foreign-keys) to
> > specify a relation between tables that don't have explicit foreign
> > keys but apparently it's not enough for join() to figure it out:
>
> > from sqlalchemy.orm import join, relation
> > from sqlalchemy import Column, Integer, String
> > from sqlalchemy.ext.declarative import declarative_base
>
> > Base = declarative_base()
>
> > class User(Base):
> >     __tablename__ = 'users'
> >     id = Column(Integer, primary_key=True)
> >     name = Column(String(50))
>
> > class Address(Base):
> >     __tablename__ = 'addresses'
> >     id = Column(Integer, primary_key=True)
> >     email = Column(String(50))
> >     user_id = Column(Integer)
> >     user = relation(User, primaryjoin=User.id==user_id,
> > foreign_keys=user_id)
>
> > # this works
> >>>> print join(User, Address, Address.user)
> > users JOIN addresses ON users.id = addresses.user_id
>
> > # this doesn't
> >>>> print join(User, Address)
> > Traceback (most recent call last):
> > ...
> > sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
> > between 'users' and 'addresses'
>
> > Why doesn't join() figure out the join condition,
>
> join() looks for foreign keys between the two tables at the schema level.
> you haven't defined that here (use ForeignKey, and then you can lose the
> "foreign_keys" parameter too).

This was just an example, in the actual case the table schema is
reflected so there's no explicit ForeignKey, that was the whole point
of messing with primaryjoin/foreign_keys in the first place. So is
there another way (or even workaround) to avoid passing explicitly the
condition ?

George
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to