its a missing feature right now. ideally we can add a rule to MySQL's datetime object that subtraction should return a type that will translate the float to an Interval (I'm guessing it's a number of days).
You can get the float right now like this:
from sqlalchemy import type_coerce
session.query((type_coerce(Foo.date1 - Foo.date2, Float)).label('diff')).all()
Or if on 0.8, a more comprehensive workaround like this:
from sqlalchemy import type_coerce, Float, TypeDecorator
class MySQLInterval(TypeDecorator):
impl = Float
def process_result_value(self, value, dialect):
# guessing, seems to be close
return datetime.timedelta(days=value / 100 / 60 / 60 / 24)
class MySQLDateTime(TypeDecorator):
impl = DateTime
class comparator_factory(TypeDecorator.Comparator):
def __sub__(self, other):
return type_coerce(type_coerce(self.expr, DateTime) - other, MySQLInterval)
Using MySQLDateTime will then treat the result of a __sub__() as a float to convert into a timedelta.