Aha i didn't realize I had a mismatch of relationship names

Below I included a full example that fails an assert where I'm having the 
issue.

   - The only reason I have "unordered_things" and "things" relationships 
   is because I wasn't sure how to get it to only include the query's sort
   - The only "requirement" I have of the 2nd query is that is the same for 
   both asc and desc sorting. Everything else was just my best attempt at 
   getting it to work
   

from sqlalchemy import asc, Column, create_engine, desc, ForeignKey, types
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import joinedload, relationship, sessionmaker

Base = declarative_base()

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(types.Integer, primary_key=True, autoincrement=True)

    parent_feature = relationship('ParentFeature', lazy='joined', uselist=
False)

class Thing(Base):
    __tablename__ = 'thing'
    id = Column(types.Integer, primary_key=True, autoincrement=True)
    name = Column(types.Unicode, nullable=False)

class ParentFeature(Base):
    __tablename__ = 'parent_feature'
    id = Column(types.Integer, primary_key=True, autoincrement=True)
    parent_id = Column(ForeignKey('parent.id'), nullable=False, index=True)

    parent = relationship('Parent', uselist=False)
    unordered_things = relationship('Thing', secondary=
'parent_feature_thing')
    things = relationship('Thing', secondary='parent_feature_thing', 
order_by=Thing.name, viewonly=True)

class ParentFeatureThingPivot(Base):
    __tablename__ = 'parent_feature_thing'
    parent_feature_id = Column(ForeignKey('parent_feature.id'), primary_key=
True)
    thing_id = Column(ForeignKey('thing.id'), primary_key=True)

engine = create_engine('sqlite:///')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

models = [
    Parent(id=1),
    Parent(id=2),
    Thing(id=1, name='aaaa'),
    Thing(id=2, name='bbbb'),
    Thing(id=3, name='cccc'),
    ParentFeature(id=1, parent_id=1),
    ParentFeature(id=2, parent_id=2),
    ParentFeatureThingPivot(parent_feature_id=2, thing_id=1),
    ParentFeatureThingPivot(parent_feature_id=2, thing_id=2),
    ParentFeatureThingPivot(parent_feature_id=2, thing_id=3),
]
for model in models:
    session.add(model)
session.flush()

parent = session.query(Parent).all()[1]
things = [thing.name for thing in parent.parent_feature.things]
things_check = ['aaaa', 'bbbb', 'cccc']
print('{} == {}'.format(things, things_check))
assert things == things_check

for direction in (asc, desc):
    things_check = ['aaaa', 'bbbb', 'cccc']
    if direction == desc:
        things_check = list(reversed(things_check))

    parent_feature = (
        session.query(ParentFeature)
        .join(ParentFeature.parent, ParentFeature.unordered_things)
        .options(
            joinedload(ParentFeature.parent),
            joinedload(ParentFeature.unordered_things),
        )
        .filter(Parent.id == 2)
        .order_by(asc(Thing.name))
        .one()
    )
    things = [thing.name for thing in parent_feature.things]
    print('{} == {}'.format(things, things_check))
    assert things == things_check


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