> -----Original Message-----
> From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
> On Behalf Of Luka Novsak
> Sent: 27 April 2011 05:32
> To: sqlalchemy
> Subject: [sqlalchemy] Appending a where clause to a query
> 
> The docs on Select's where() method say:
> 
> > return a new select() construct with the given expression added to
> its WHERE clause, joined to the existing clause via AND, if any.
> 

Note: "return a new select() construct"

> But this doesn't seem to happen.
> 
> This is my code:
> 
>     def posts_per_dow(self, start_date=None, end_date=None):
>         q = select([func.date_part('isodow', t_posts.c.created_at),
>                     func.count(t_posts.c.id)],
> 
> t_posts.c.user_id==self.id).group_by('1').order_by('1')
> 
>         if start_date: q.where(t_posts.c.created_at>=start_date)
>         if end_date: q.where(t_posts.c.created_at<end_date)
> 
> Only the first where clause is actually used when I execute the query.
> 
> If I'm just going about it wrong, then how do I append a where clause
> like this?
> 

You need to store the return value of the 'where' method. eg:

if start_date:
    q = q.where(t_posts.c.created_at>=start_date)
if end_date:
    q = q.where(t_posts.c.created_at<end_date)


Hope that helps,

Simon

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

Reply via email to