Hi,
When subtracting DateTime columns in query,
like: session.query((Foo.datetime1 - Foo.datetime2).label('diff'))
SQLAlchemy tries to handle result as timedelta but MySQL returns float as a
result so it crashes with "TypeError: unsupported operand type(s) for -:
'float' and 'datetime.datetime'" exception.
So I wondering if it's a bug or it should never be queried like that and
proper function for subtracting datetimes should be always used instead?
Tested on fresh cloned from repository SQLAlchemy version.
Example code is attached to this message.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
import datetime
from sqlalchemy import ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, DateTime
Base = declarative_base()
engine = create_engine('mysql://test:test@localhost/test', echo=True)
Session = sessionmaker(autoflush=False, bind=engine)
class Foo(Base):
__tablename__ = 'foos'
id = Column(
Integer(),
primary_key=True
)
date1 = Column(DateTime())
date2 = Column(DateTime())
session = Session()
Base.metadata.create_all(engine)
session.add(
Foo(
date1=datetime.datetime(2013, 6, 1, 1, 30, 40),
date2=datetime.datetime(2013, 5, 23, 1, 30, 40)
)
)
session.commit()
session.query((Foo.date1 - Foo.date2).label('diff')).all()