So, I have a model that is something like:
class Foo(model):__tablename__ = "foo"id = Column(Integer, primary_key=True)data = relationship("FooData",cascade="all, delete-orphan",backref="foo",lazy="dynamic",order_by="desc(FooData.timestamp)")@propertydef first_item(self):# the problem is here:return self.data.order_by(asc("timestamp")).first()@propertydef latest_item(self):return self.data.first()class FooData(Model):__tablename__ = "foo_data"foo_id = Column(Integer, ForeignKey("foo.id"), primary_key=True)timestamp = Column(DateTime, primary_key=True)actual_data = Column(Float, nullable=False)
So, the problem is with the first_item
method there: when it is defined as above, the SQL looks like this:
SELECT foo_data.timestamp AS foo_data_timestamp, foo_data.actual_data AS foo_data_actual_data, foo_data.foo_id AS foo_data_foo_id
FROM foo_data
WHERE :param_1 = foo_data.foo_id ORDER BY foo_data.timestamp DESC, foo_data.timestamp ASC
-- ^^^^^^^^^^^^^^^^^^^^^^
Obviously, the order_by
specified in the query is being appended to the one specified in the relationship definition, instead of replacing it; is there a way for a query to override the original order_by
? I know I could specify a separate query directly on the FooData
class, but I would like to avoid that if possible.