title: How to add if condition in this query?
body: <p>I'm using flask_sqlalchemy.</p>
  
  <p>I have 10 students in 2 classes with fees.</p>
  
  <p>When <code>school_class_id</code> is passed I only need to return the students from specific class else all the students.</p>
  
  <p>The first query works fine for listing all students.
  In the second code there added <code>school_class_id</code> also listing as expected for listing students from a specific class.</p>
  
  <p>But when I add a <code>school_class_id</code> filter in <code>if</code> condition then it is returning only one student.</p>
  
  <p>This code works fine in fetching all students:</p>
  
  <pre><code>result = db.session.query(
                  Student, SchoolClass, Guardian,
                  func.sum(StudentFee.net_payable).label('total_payable'),
                  ).filter(
                      and_(SchoolClass.school_id == user['school_id'],
                      SchoolClass.id == Student.school_class_id),
                      SchoolClass.status == GenEnum.ACTIVE.value).join(
                          StudentGuardian, StudentGuardian.student_id == Student.id).filter(
                              Guardian.id == StudentGuardian.guardian_id
              ).outerjoin(StudentFee,
                  StudentFee.student_id == Student.id
                          ).filter(StudentFee.status == NewEnum.ACTIVE.value).group_by(Student.id)
  
  result.all()
  </code></pre>
  
  <p>This code works when passing the <code>school_class_id</code>:</p>
  
  <pre><code>result = db.session.query(
                  Student, SchoolClass, Guardian,
                  func.sum(StudentFee.net_payable).label('total_payable'),
                  ).filter(
                      and_(SchoolClass.school_id == user['school_id'],
                      # SchoolClass.id == body.get('school_class_id'),
                      SchoolClass.id == Student.school_class_id),
                      SchoolClass.status == GenEnum.ACTIVE.value).join(
                          StudentGuardian, StudentGuardian.student_id == Student.id).filter(
                              Guardian.id == StudentGuardian.guardian_id
              ).outerjoin(StudentFee,
                  StudentFee.student_id == Student.id
                          ).filter(StudentFee.status == NewEnum.ACTIVE.value).group_by(Student.id)
  
  result.all()
  </code></pre>
  
  <p>But this code adding filter later has some issue as it's only returning one student:</p>
  
  <pre><code>result = db.session.query(
                  Student, SchoolClass, Guardian,
                  func.sum(StudentFee.net_payable).label('total_payable'),
                  )
  
  if body.get('school_class_id'):
      result.filter(SchoolClass.id == body.get('school_class_id'))
  
  result.filter(
          and_(SchoolClass.school_id == user['school_id'],
          SchoolClass.id == Student.school_class_id),
          SchoolClass.status == GenEnum.ACTIVE.value).join(
              StudentGuardian, StudentGuardian.student_id == Student.id).filter(
                  Guardian.id == StudentGuardian.guardian_id
  ).outerjoin(StudentFee,
      StudentFee.student_id == Student.id
              ).filter(StudentFee.status == NewEnum.ACTIVE.value).group_by(Student.id)
  
  
  result.all()
  </code></pre>