Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlalchemy as sa
- from sqlalchemy import orm
- from sqlalchemy.ext.declarative import declarative_base
- Base = declarative_base()
- class Parent(Base):
- __tablename__ = "parent"
- id = sa.Column(sa.Integer, primary_key=True)
- expr = orm.column_property(id > 0)
- children = orm.relationship("Child", back_populates="parent")
- class Child(Base):
- __tablename__ = "child"
- id = sa.Column(sa.Integer, primary_key=True)
- parent_id = sa.Column(sa.Integer, sa.ForeignKey("parent.id"))
- parent = orm.relationship("Parent", back_populates="children")
- engine = sa.create_engine("postgres:///test", echo=True)
- Base.metadata.create_all(engine)
- session = orm.Session(bind=engine)
- q = session.query(Parent)
- q = q.options(orm.joinedload(Parent.children))
- q = q.order_by(Parent.expr)
- # Workaround:
- #q = q.order_by(sa.case([(Parent.expr, 1)], else_=0))
- q = q.limit(10)
- q.all()
Advertisement
Add Comment
Please, Sign In to add comment