title: SqlAlchemy filter by time difference
body: <p>I'm new (but not so new) to SQLAlchemy. I'm using version 0.9.3 for a project. I want to make a query to the SQLite database filtering the result to get those objects with no time stamp or have more than 24 hours since their last update (on the same column).</p>
  
  <p>The problem is, I have no exact idea of how achieve the time filtering part, I did a IPython Notebook session, so people can look how long a tried looking for an answer to my issue:</p>
  
  <p>In [1]:</p>
  
  <pre><code>from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Column, Unicode, DateTime, Integer, create_engine
  from sqlalchemy.orm import sessionmaker, relationship, backref
  from datetime import datetime
  
  engine = create_engine('sqlite:///:memory:')
  Base = declarative_base()
  Session = sessionmaker(bind=engine)
  session = Session()
  </code></pre>
  
  <p>In [2]:</p>
  
  <pre><code>class Product(Base):
      __tablename__ = "product"
  
      id = Column(Integer, primary_key=True)
      name = Column(Unicode(140), unique=True, nullable=False)
      department = Column(Unicode(20))
      added = Column(DateTime, default=datetime.now)
      last_time_parsed = Column(DateTime)
  
      def repr(self):
          return "&lt;Product(name=%s, department=%s, added=%s, last_time_parsed=%s)&gt;" % (
                              self.name, self.department, self.added, self.last_time_parsed)
  </code></pre>
  
  <p>In [3]:</p>
  
  <pre><code># create the tables
  Base.metadata.create_all(engine)
  </code></pre>
  
  <p>In [4]:</p>
  
  <pre><code># create a false product
  p1 = Product(name="Product X")
  p2 = Product(name = "Product Y")
  p3 = Product(name = "Product Z")
  </code></pre>
  
  <p>In [5]:</p>
  
  <pre><code>session.add(p1)
  session.add(p2)
  session.add(p3)
  session.commit()
  
  /home/jorge/projects/project1/lib/python2.7/site-packages/sqlalchemy/engine/default.py:525: SAWarning: Unicode type received non-unicode bind param value.
    param.append(processors[key](compiled_params[key]))
  </code></pre>
  
  <p>In [7]:</p>
  
  <pre><code>q = session.query(Product).filter(Product.last_time_parsed == None).filter(Product.last_time_parsed &gt; 24)
  </code></pre>
  
  <p>In [9]:</p>
  
  <pre><code>print q.first().name
  
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-9-377361de0bab&gt; in &lt;module&gt;()
  ----&gt; 1 print q.first().name
  
  AttributeError: 'NoneType' object has no attribute 'name'
  </code></pre>
  
  <p>In [14]:</p>
  
  <pre><code>q = session.query(Product).filter(Product.last_time_parsed == None)
  </code></pre>
  
  <p>In [15]:</p>
  
  <pre><code>print q.first().name
  
  Product X
  </code></pre>
  
  <p>In [16]:</p>
  
  <pre><code>q = session.query(Product).filter(Product.last_time_parsed &lt; 24)
  print q.first().name
  
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-16-b10ccbb5d88d&gt; in &lt;module&gt;()
        1 q = session.query(Product).filter(Product.last_time_parsed &lt; 24)
  ----&gt; 2 print q.first().name
  
  AttributeError: 'NoneType' object has no attribute 'name'
  </code></pre>
  
  <p>In [20]:</p>
  
  <pre><code>q = session.query(Product).filter(Product.added &gt; 24)
  print q.first().name
  
  Product X
  </code></pre>
  
  <p>In []:</p>
  
  <p>In <code>input 20</code> I know that <code>Product.added</code> is a DateTime data type (and I used just to test because is the only column holding DateTime values). What I don't understand is the logic statement. If were <code>Product.added &lt; 24</code> (as shown on <code>input 16</code>) instead of <code>Product.added &gt; 24</code> I would get no object from the database. So, by the number <code>24</code> what is actually the database understanding? 24 Minutes? 24 Hours? 24 Seconds? 24 Days?</p>
  
  <p>So, again, How do I filter data to get data that have no time stamp or that was updated more than 24 hours ago?</p>
  
  <p>Thanks :)</p>