Hello everyone.

I am trying to get classes whose foreign key is "whatever" but I
always get all the entries in the database, instead of the ones that
match the criterion.

Let's say I have a couple of classes using declarative base in a
relationship N:1. I have that modeled like:

class OtherClass(declarativeBase):
        __tablename__ = "other_classes"
        _id = Column("id", Integer, primary_key=True)
        id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))


class WhateverClass(declarativeBase):
        __tablename__ = "whatever_classes"

        _id = Column("id", Integer, primary_key=True)
        _total = Column("total", Integer)
        _otherClassId = Column("other_class_id", Integer,
ForeignKey("other_classes.id"))

        _otherClass = relationship("Post", uselist=False)

        id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
        total = sqlalchemy.orm.synonym('_total',
descriptor=property(getTotal, setTotal))
        otherClassId = sqlalchemy.orm.synonym('_otherClassId',
descriptor=property(getOtherClassId, setOtherClassId))
        otherClass = sqlalchemy.orm.synonym('_otherClass',
descriptor=property(getOtherClass setOtherClass))

If I try to do:
    from myClasses import WhateverClass
    session.query(WhateverClass.WhateverClass).filter(otherClassId <= 5).all()

I get a list with all the instances of WhateverClass that are stored
in the database, not only the ones who are linked to the OtherClass
with id <= 5

But if I do session.query(WhateverClass.WhateverClass).filter(total <=
100).all() I properly get instances of WhateverClass with a "total"
<=100.

Do I have to use a joined query or something like that?

Thank you in advance!

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to