[sqlalchemy] Re: Building an or_ filter in loop
this would even be easier (and correcter if the terms contain %) written as: cond = or_(*[Fruit.name.contains(term) for term in terms]) On 8 mei, 21:59, Kyle Schaffrick k...@raidi.us wrote: On Fri, 8 May 2009 12:52:09 -0700 (PDT) Bryan bryanv...@gmail.com wrote: I can't figure out a clean way of adding a bunch of filter terms to a query in a loop joined by an OR clause. Successive calls to filter join the expressions by AND. I would like to do something like the following, but have the expressions joined by OR terms = ['apple', 'orange', 'peach'] q = Session.query(Fruit) for term in terms: q = q.filter(Fruit.name.like('%' + term + '%') Desired pseudo-sql: SELECT * FROM fruit WHERE name like '%apple%' OR name like '%orange%' OR name like '%peach%' I think this might do what you want: cond = or_(*[ Fruit.name.like('%' + term + '%') for term in terms ]) q = Session.query(Fruit).filter(cond) -Kyle --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Building an or_ filter in loop
On Mon, 11 May 2009 09:01:06 -0700 (PDT) polaar steven.vereec...@gmail.com wrote: this would even be easier (and correcter if the terms contain %) written as: cond = or_(*[Fruit.name.contains(term) for term in terms]) Indeed, good catch. I was so interested in the apply or_ logic that I missed the escaping bug :) -Kyle --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Building an or_ filter in loop
On Fri, 8 May 2009 12:52:09 -0700 (PDT) Bryan bryanv...@gmail.com wrote: I can't figure out a clean way of adding a bunch of filter terms to a query in a loop joined by an OR clause. Successive calls to filter join the expressions by AND. I would like to do something like the following, but have the expressions joined by OR terms = ['apple', 'orange', 'peach'] q = Session.query(Fruit) for term in terms: q = q.filter(Fruit.name.like('%' + term + '%') Desired pseudo-sql: SELECT * FROM fruit WHERE name like '%apple%' OR name like '%orange%' OR name like '%peach%' I think this might do what you want: cond = or_(*[ Fruit.name.like('%' + term + '%') for term in terms ]) q = Session.query(Fruit).filter(cond) -Kyle --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Building an or_ filter in loop
That worked, thanks On May 8, 12:59 pm, Kyle Schaffrick k...@raidi.us wrote: On Fri, 8 May 2009 12:52:09 -0700 (PDT) Bryan bryanv...@gmail.com wrote: I can't figure out a clean way of adding a bunch of filter terms to a query in a loop joined by an OR clause. Successive calls to filter join the expressions by AND. I would like to do something like the following, but have the expressions joined by OR terms = ['apple', 'orange', 'peach'] q = Session.query(Fruit) for term in terms: q = q.filter(Fruit.name.like('%' + term + '%') Desired pseudo-sql: SELECT * FROM fruit WHERE name like '%apple%' OR name like '%orange%' OR name like '%peach%' I think this might do what you want: cond = or_(*[ Fruit.name.like('%' + term + '%') for term in terms ]) q = Session.query(Fruit).filter(cond) -Kyle --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---