title: SQLAlchemy datetime operations on server side
body: <p>I have a table with scheduled and past payments, and I need to find if there are any two charges made in the same week for the same user/contract.</p>
  
  <pre><code>select count(*) from charge as c1, charge as c2 
  where c1.id_contract = c2.id_contract 
    and c1.status = 'SUCCESS' 
    and c2.status in ('SUCCESS', 'PENDING', 'WAITING') 
    and c1.id &gt; c2.id and c2.due_time &gt; (c1.due_time - interval 7 day);
  </code></pre>
  
  <p>I'm having a hard time at reproducing this query in sqlalchemy, mainly because I can't find how to translate MySQL's 'interval' to SQLAlchemy in a database agnostic form.</p>
  
  <p>So far I came up with this, which translates everything, but the interval:</p>
  
  <pre><code>db.session.query(Charge, OldCharge).filter(Charge.id_contract == OldCharge.id_contract, Charge.status=='WAITING', OldCharge.status.in_(('SUCCESS', 'PENDING')), Charge.id &gt; OldCharge.id).count()
  </code></pre>
  
  <p>Any ideas?</p>