title: Deleting list of items in SQLAlchemy Flask
body: <p>I'm trying to delete a list of items from using Flask and SQLAchlemy in an elegant way. In my case, I want to delete old items (items that are less than a certain time are considered old) from my database.</p>
  
  <p>Here is my python model object:</p>
  
  <pre><code>class ItemModel(db.Model):
      __tablename__ = 'items'
  
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80))
      time = db.Column(db.Integer)
  </code></pre>
  
  <p>Now, I tried to delete the items using the following filter_by query:</p>
  
  <pre><code>def delete_from_db(self):
      db.session.query('items').filter_by(self.time &lt;= epoch_time).delete()
      db.session.commit()
  </code></pre>
  
  <p>It just ends up giving me the error: </p>
  
  <blockquote>
    <p>filter_by() takes 1 positional argument but 2 were given</p>
  </blockquote>
  
  <p>If I try this using the filter function, instead of filter_by, I also receive an error: </p>
  
  <pre><code>   def delete_from_db(self):
          db.session.query('items').filter(self.time &lt;= epoch_time).delete()
          db.session.commit()
  </code></pre>
  
  <blockquote>
    <p>sqlalchemy.exc.InvalidRequestError: This operation requires only one
    Table or entity be specified as the target.</p>
  </blockquote>
  
  <p>Finally, I decide to go back to the basics and use sql code and this works but I don't think it is very elegant:</p>
  
  <pre><code>def delete_from_db(self):
      from sqlalchemy.sql import text
      cmd = 'delete from items where time &lt;= :time'
      db.engine.execute(text(cmd), time = epoch_time)
      db.session.commit()
  </code></pre>
  
  <p>Is there a more elegant way to do this with Flask and SQLAchlemy?</p>