title: sqlalchemy multiple nested queries with filters from one table AttributeError: 'tuple' object has no attribute 'filter'
body: <p>I have one table,</p>
  
  <pre><code>class Sale(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      vente_no = db.Column(db.String(50))
      lampshade = db.Column(db.String(30))
      brocante = db.Column(db.String(30))
      buyer = db.Column(db.String(255))
      pays = db.Column(db.String(255))
      QT = db.Column(db.Integer)
      prix_de_vente = db.Column(db.Float)
      poste = db.Column(db.Float)
      total = db.Column(db.Float)
      bordeaux = db.Column(db.Integer)
      amazon = db.Column(db.String(255))
      bordeaux_euros = db.Column(db.Float)
      bordeaux_comm = db.Column(db.Float)
      amazon_euros = db.Column(db.Float)
      amazon_comm = db.Column(db.Float)
      PID = db.Column(db.Integer, db.ForeignKey("period.id"))
      date = db.Column(db.Date)
      description = db.Column(String(255))
  
      P_id = db.relationship("Period")
  
      def __repr__(self):
          return '{}'.format(self.id)
  </code></pre>
  
  <p>I need to create one query with two parameters and filters so that I can
  iterate through the rows. I am following this tutorial <a href="https://blog.miguelgrinberg.com/post/nested-queries-with-sqlalchemy-orm" rel="nofollow noreferrer">nested queries</a>  which I am 
  following, here is my subquery</p>
  
  <pre><code>poste_eu = db.session.query(
      (Sale.id, Sale.poste, Sale.pays).filter(or_(Sale.pays=='United Kingdom', Sale.pays=='France', Sale.pays=='Germany',\
             Sale.pays=='The Netherlands', Sale.pays=='Italy', Sale.pays=='Slovakia', Sale.pays=='Ireland',\
             Sale.pays=='Malta', Sale.pays=='Belgium', Sale.pays=='Spain')).label('poste_eu')
      ).group_by(Sale.id.asc()).subquery()
  </code></pre>
  
  <p>But in terminal this gives the following error</p>
  
  <blockquote>
  <pre><code>&gt;&gt;&gt; poste_monde = db.session.query(
    ...     (Sale.id, Sale.poste, Sale.pays).filter(~or_(Sale.pays=='United Kingdom', 
       Sale.pays=='France', Sale.pays=='Germany',\
    ...            Sale.pays=='The Netherlands', Sale.pays=='Italy', Sale.pays=='Slovakia', 
       Sale.pays=='Ireland',\
    ...            Sale.pays=='Malta', Sale.pays=='Belgium', 
      Sale.pays=='Spain')).label('poste_monde')
    ...     ).group_by(Sale.id.asc()).subquery()
    Traceback (most recent call last):
      File "&lt;stdin&gt;", line 2, in &lt;module&gt;
    AttributeError: 'tuple' object has no attribute 'filter' ""
  </code></pre>
  </blockquote>
  
  <p>this same query written like this works</p>
  
  <pre><code>def poste_monde(pid, month):
      p_monde = Sale.query.with_entities(Sale.id, Sale.poste).filter(Sale.PID==pid, extract('month', Sale.date)==month,\
            ~or_(Sale.pays=='United Kingdom', Sale.pays=='France', Sale.pays=='Germany', Sale.pays=='The Netherlands',\
             Sale.pays=='Italy', Sale.pays=='Slovakia', Sale.pays=='Ireland', Sale.pays=='Malta', Sale.pays=='Belgium',\
             Sale.pays=='Spain', Sale.pays=='FR', Sale.pays=='GB')).all()
      return p_monde
  </code></pre>
  
  <p>I have looked at <a href="https://stackoverflow.com/questions/11578070/sqlalchemy-instrumentedlist-object-has-no-attribute-filter">this</a> question and wonder if I should have my classes set
  up in a similar way with lazy relationships.</p>
  
  <p>my query will have nine subqueries like the example shown, the Paremeters
  are 'pid' and 'month' and the filters are a list of countries</p>
  
  <p>I need help to get the first query working so that I can add the rest!!!?
  warm regards
  Paul</p>