Firstly, thanks for making this software available.

My question is related to this issue, probably a common one for newbies :

Consider the following entities :

class Foo(Base):
    __tablename__ = "foo"
    id = Column(Integer, primary_key=True)
    name = Column(String, unique = True)
class Bar(Base):
    __tablename__ = "bar"
    id = Column(Integer, primary_key = True)
    foo_id = Column(Integer, ForeignKey(""))

    foo = relationship("Foo", lazy="joined")

ses.query(Bar).filter( == "blah") throws

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' 
object associated with 

has an attribute 'name'

( Note : i have edited the example somewhat )

>From the answers, it seems that i cannot use, since foo is a 

Also, i have to explicitly join Foo to the query in order to use its attributes 
in filter or order-by.

However, when i do this, and i have lazy="joined" in the relationship, 

the relation too creates a join (which cannot be used in the filter, orderby),

plus the join i have already added in the query.

My questions are related to this :

1. Why don't we allow the declared relations to be available in the query for 
filter/order-by etc ?

Does this not undermine the power of declarative relations ?

Can we declare a list of columns to be fetched for a relation ?

2. Given that we might have to add filter conditions to queries over time, what 
is the best-practice for loading

declared relations ? Should they have lazy="noload" or default(lazy-loading) 

and we use explicit joins in queries to avoid the duplicate joins ?

3. If i add a filter in the query for Foo without creating a join, it adds Foo 
in the FROM clause, 

but without any join condition.

Is this a bug ?



SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See for a full description.
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
To post to this group, send email to
Visit this group at
For more options, visit

Reply via email to