title: sql alchemy python query query.first() _and
body: <p>I am modifying some code I did not write.  The problem with this code is that tracking numbers have started to recycle with the carrier.  So for a given tracking number there may be two sometimes 3 rows in the database.  This code apparently finds the first record in the query which in our case is the 'oldest created date'.   </p>
  
  <p>So I would like to understand how to change this to one of the  2 options I am thinking so the program only pulls the most recent tracking number.</p>
  
  <ol>
  <li>Retrieve the last record of the query and place it in manifest variable.</li>
  <li>use an 'AND' on the query filter.  I have a created_date column that I can say:
  if tracking number = exception tracking number AND created date > 1/1/20011.</li>
  </ol>
  
  <p>So if someone could provide the code that does #1 and #2 I would be grateful.</p>
  
  <pre><code>query = session.query(database.Manifest)
  query = query.filter(database.Manifest.tracking_number==exception.TrackingNumber)
  
  manifest = query.first()
  </code></pre>
  
  <p>I thought for number 2, the statement could be:</p>
  
  <pre><code>query = query.filter(_and(database.Manifest.tracking_number==exception.TrackingNumber, 
  database.Manifest.created_date &gt; '2011-01-01'))
  </code></pre>
  
  <p>ok?</p>