-
-
Save ThiefMaster/d6c16d26c77507612b0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import * | |
from sqlalchemy.orm import * | |
from sqlalchemy.ext.declarative import declarative_base | |
Base = declarative_base() | |
class A(Base): | |
__tablename__ = 'test_a' | |
id = Column(Integer, primary_key=True) | |
class B(Base): | |
__tablename__ = 'test_b' | |
id = Column(Integer, primary_key=True) | |
a_id = Column(Integer, ForeignKey('test_a.id')) | |
c_id = Column(Integer, ForeignKey('test_c.id')) | |
a = relationship('A', backref=backref('b')) | |
c = relationship('C', backref=backref('b')) | |
class C(Base): | |
__tablename__ = 'test_c' | |
id = Column(Integer, primary_key=True) | |
e = create_engine('sqlite:///:memory:', echo=True) | |
Base.metadata.create_all(e) | |
s = Session(e) | |
s.add(B(a=A(), c=C())) | |
s.commit() | |
a = s.query(A).options(joinedload('b').noload('c')).all() | |
print a[0] | |
print a[0].b[0] | |
print a[0].b[0].c |
Author
ThiefMaster
commented
Aug 11, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment