Is there any way to get relationship joined loading to work from child back 
up to the parent?

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///:memory:')
Base = declarative_base()


class GrandParent(Base):
    __tablename__ = 'grands'

    id = Column(Integer, primary_key=True)
    name = Column(String())


class Parent(Base):
    __tablename__ = 'parents'

    gp_id = Column(ForeignKey(GrandParent.id, ondelete='cascade'), nullable=
False)
    gp = relationship(GrandParent, lazy='joined', innerjoin=True)

    id = Column(Integer, primary_key=True)
    name = Column(String())


class Child(Base):
    __tablename__ = 'childs'

    parent_id = Column(ForeignKey(Parent.id, ondelete='cascade'), nullable=
False)
    parent = relationship(Parent, lazy='joined', innerjoin=True)

    id = Column(Integer, primary_key=True)
    name = Column(String())


Base.metadata.create_all(engine)
engine.echo = True

Session = sessionmaker(bind=engine)
session = Session()

gp = GrandParent(name='foo')
p = Parent(name='bar', gp=gp)
c = Child(name='baz', parent=p)

session.add(c)
session.commit()

# How many SQL statements?  Why not 1 that would load Child, it's parent, 
and it's grand parent?
assert c.parent.gp.id

As the last comment says, any way to get that last statement to issue one 
join with all the inner joins to load the parent objects?

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to