Hello, given the following base:
class Bar(Base):
__tablename__ = 'bars'
id = Column(Integer, primary_key=True)
class Foo(Base):
__tablename__ = 'foos'
id = Column(Integer, primary_key=True)
bar_id = Column(Integer, ForeignKey(Bar.id))
bar = relationship(Foo, backref='bar')
I wonder why sqlalchemy allows this kind of ORM queries to be executed:
session.query(Foo).filter(Bar.id == 42)
that results in the execution of this SQL:
SELECT foos.id AS foos_id, foos.bar_id AS foos_bar_id
FROM foos, bars
WHERE bars.id = ?
This is way more often a mistake than what the developer originally intended. I am asking this question because it is quite easy to add a filter condition to a model that was not originally joined, effectively voiding the filter condition:
session.query(Foo).join(Foo.bar).filter(Bar.id==42) # Good query
session.query(Foo).filter(Bar.id==42) # Oops, I forgot to join Foo.bar.
In the above example, the second query will fetch all the Foos from the database. This is both a very slow operation and it also return unwanted records. Maybe it should raise instead?
EDIT: Fixed the last example
Hello, given the following base:
I wonder why sqlalchemy allows this kind of ORM queries to be executed:
that results in the execution of this SQL:
This is way more often a mistake than what the developer originally intended. I am asking this question because it is quite easy to add a filter condition to a model that was not originally joined, effectively voiding the filter condition:
In the above example, the second query will fetch all the
Foos from the database. This is both a very slow operation and it also return unwanted records. Maybe it should raise instead?EDIT: Fixed the last example