python - SQLAlchery query filter on child attribute -


my model consists of parent , child one-to-one relationship:

class parent(base):     __tablename__ = 'parent'     id = column(integer, primary_key=true)     name = column(string)     child = relationship("child", backref="parent", uselist=false, lazy='joined')   class child(base):     __tablename__ = 'child'     child_id = column(integer, foreignkey(parent.id), primary_key=true)     value = column(integer) 

my test data following:

q = s.query(parent) pd.read_sql(q.statement,s.bind)      id  name  child_id  value     1              1     10     2      b         2     20     3      c         3     30 

now i'd parents child.value > 20 using query:

q = s.query(parent).filter(parent.child.value > 20) 

but error occurs:

attributeerror: neither 'instrumentedattribute' object nor 'comparator' object  associated parent.child has attribute 'value' 

of course can query direct on child class goal retrieve parent object.

you should change query.

# version-1: use join q = s.query(parent).join(child, parent.child).filter(child.value > 20)  # or: # version-2: use exists q = s.query(parent).filter(parent.child.has(child.value > 20)) 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -