Custom Search

Tuesday, September 21, 2010

and_ or_ not_ in_ sqlalchemy examples

sqlalchemy examples and_ or_ not_ in_

from sqlalchemy.sql.expression import or_, not_, and_

--------- AND_

DBSession.query(VM).filter(and_(and_(VM.id == EntityRelation.dest_id, EntityRelation.src_id == self.id), EntityRelation.relation == u'Children')).options(eagerload("current_state")):

Equal to:
DBSession.query(VM).filter((VM.id == EntityRelation.dest_id AND EntityRelation.src_id == self.id) AND EntityRelation.relation == u'Children')).options(eagerload("current_state")):

---------- OR_

DBSession.query(DWM,DWMPolicySchedule).filter(DWM.sp_id==sp_id).join((DWMPolicySchedule,DWMPolicySchedule.policy_id==DWM.id)).filter(or_(DWMPolicySchedule.start <= time,DWMPolicySchedule.end >= time)).all()

Equal to:
DBSession.query(DWM,DWMPolicySchedule).filter(DWM.sp_id == sp_id).join((DWMPolicySchedule,DWMPolicySchedule.policy_id==DWM.id)).filter(DWMPolicySchedule.start <= time OR PolicySchedule.end >= time)).all()

---------- NOT_ IN_

DBSession.query(PSDownServers).filter(not_(PSDownServers.node_id.in_(child_ids))).filter(PSDownServers.sp_id == sp_id).delete()

Equal to:
DBSession.query(PSDownServers).filter(PSDownServers.node_id.Not_in_(child_ids)).filter(PSDownServers.sp_id == sp_id).delete()

No comments:

Post a Comment