[FIXED] How to filter an sqlalchemy query to all Parents w/o Children, and all Parents, who fall under conditions in a Flask Form

Issue

To begin with, I am very new to coding, so sorry in advance if it is not worth attention.

I work with one to many relationship. Let’s say I have a Parent class and a Child class defined as follows:

class Parent(db.Model):
    __tablename__ = 'parent'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable = False)   
    age = db.Column(db.Integer(32), nullable = False) 
    children = db.relationship('Child', backref='parent', lazy='dynamic')


class Child(db.Model):
    __tablename__ = 'child'
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'), nullable = False) 
    weight = db.Column(db.Integer(32), nullable = False)

What I want to do is outerjoin the tables and display info (name, age) of all parents who:

  1. Either do not have children
  2. Or both parents and children satisfy the conditions previously entered through a Flask form (user submits a range (let’s say minimal and max weight of a child and same for age)). Parent appears in the result if they have at least one child that satisfies the conditions and the parent themselves satisfy it.

I succeeded in getting a query that satisfies either 1 or 2. These where the queries:

#1 Works!

parentssql=session.query(Parent, Child)\
.outerjoin(Child)\
.filter(Child.id == None)\
.distinct(Parent.name)\
.group_by(Parent.name)\
.order_by(Parent.name)\
.all()

#2 Also works!

parents=session.query(Parent, Child)\
.outerjoin(Child)\
.filter(Parent.age.between(form.age_min.data, form.age_max.data), Child.weight.between(form.weight_min.data, form.weight_max.data))\
.distinct(Parent.name)\
.group_by(Parent.name)\
.order_by(Parent.name)\
.all()

So how do I combine those without making too many queries (basically as efficient as possible (question mark))

Thank you!

EDIT 1:

I tried using or and and conditions but it gave me an error. First I will add my edited code (originally it was about galaxies and line detections)

The edited query:

galaxies=session.query(Galaxy, Line)\
.outerjoin(Line)\
.filter(Galaxy.name.contains(form_advanced.name.data) \
& (Galaxy.right_ascension.between(form_advanced.right_ascension_min.data, form_advanced.right_ascension_max.data) | Galaxy.right_ascension == None ) \
& (Galaxy.declination.between(form_advanced.declination_min.data, form_advanced.declination_max.data) | Galaxy.declination == None ) \
& (Galaxy.redshift.between(form_advanced.redshift_min.data, form_advanced.redshift_max.data) | Galaxy.redshift == None ) \
& (Galaxy.lensing_flag.contains(form_advanced.lensing_flag.data) | Galaxy.lensing_flag == None))

Here is where I add kinda condition (Child.id == None) | (condition1 & condition2 & …)

galaxies = galaxies.filter((Line.id == None) | ((Line.j_upper.between(form_advanced.j_upper_min.data, form_advanced.j_upper_max.data) | Line.j_upper == None ) \
& (Line.line_id_type.contains(form_advanced.line_id_type.data) | Line.line_id_type == None) \
& (Line.integrated_line_flux.between(form_advanced.integrated_line_flux_min.data, form_advanced.integrated_line_flux_max.data) | Line.integrated_line_flux == None) \
& (Line.peak_line_flux.between(form_advanced.peak_line_flux_min.data, form_advanced.peak_line_flux_max.data) | Line.peak_line_flux == None) \
& (Line.line_width.between(form_advanced.line_width_min.data, form_advanced.line_width_max.data) | Line.line_width == None ) \
& (Line.observed_line_frequency.between(form_advanced.observed_line_frequency_min.data, form_advanced.observed_line_frequency_max.data) | Line.observed_line_frequency == None ) \
& (Line.detection_type.contains(form_advanced.detection_type.data) | Line.detection_type == None) \
& (Line.observed_beam_major.between(form_advanced.observed_beam_major_min.data, form_advanced.observed_beam_major_max.data) | Line.observed_beam_major == None ) \
& (Line.observed_beam_minor.between(form_advanced.observed_beam_minor_min.data, form_advanced.observed_beam_minor_max.data) | Line.observed_beam_minor == None ) \
& (Line.reference.contains(form_advanced.reference.data), Line.reference == None) ))
galaxies = galaxies.distinct(Galaxy.name).group_by(Galaxy.name).order_by(Galaxy.name).all()

And that was the error I was getting:

sqlalchemy.exc.ArgumentError: SQL expression for WHERE/HAVING role expected, got (<sqlalchemy.sql.elements.BinaryExpression object at 0x7f381585b790>, <sqlalchemy.sql.elements.BinaryExpression object at 0x7f3814eb6670>).

EDIT 2

Solved the issue and made it work with the comments from @vitaliy below!

Solution

You can just add more criteria to filter function joining then with or_ and and_:

parents = session.query(Parent, Child)\
.outerjoin(Child)\
.filter(or_(and_(Parent.age.between(age_min, age_max), Child.weight.between(weight_min, weight_max)), Child.id == None))\
.distinct(Parent.name)\
.group_by(Parent.name)\
.order_by(Parent.name)\
.all()

I’m not sure what was the intention behind or_ in your code, but now it’s doing nothing because only one argument is passed to or_ function.
If you meant that Child.weight or Parent.age should satisfy provided conditions, then your filter should look like this:

filter(or_(Parent.age.between(age_min, age_max), Child.weight.between(weight_min, weight_max), Child.id == None))

I would also suggest making your code less redundant and more Pythonic:

parents = session.query(Parent).outerjoin(Child)\
  .filter((Parent.age.between(age_min, age_max) & Child.weight.between(weight_min, weight_max)) | (Child.id == None))\
  .order_by(Parent.name)\
  .all()

Answered By – vitaliy

Answer Checked By – Laura B. (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published