avoidscorn

Boolean-typed column_property test case

Nov 28th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import sqlalchemy as sa
  2. from sqlalchemy import orm
  3. from sqlalchemy.ext.declarative import declarative_base
  4.  
  5. Base = declarative_base()
  6.  
  7. class Parent(Base):
  8.     __tablename__ = "parent"
  9.  
  10.     id = sa.Column(sa.Integer, primary_key=True)
  11.     expr = orm.column_property(id > 0)
  12.  
  13.     children = orm.relationship("Child", back_populates="parent")
  14.  
  15. class Child(Base):
  16.     __tablename__ = "child"
  17.    
  18.     id = sa.Column(sa.Integer, primary_key=True)
  19.     parent_id = sa.Column(sa.Integer, sa.ForeignKey("parent.id"))
  20.    
  21.     parent = orm.relationship("Parent", back_populates="children")
  22.  
  23. engine = sa.create_engine("postgres:///test", echo=True)
  24. Base.metadata.create_all(engine)
  25.  
  26. session = orm.Session(bind=engine)
  27. q = session.query(Parent)
  28. q = q.options(orm.joinedload(Parent.children))
  29. q = q.order_by(Parent.expr)
  30. # Workaround:
  31. #q = q.order_by(sa.case([(Parent.expr, 1)], else_=0))
  32. q = q.limit(10)
  33. q.all()
Advertisement
Add Comment
Please, Sign In to add comment