【发布时间】:2022-01-16 19:45:10
【问题描述】:
我在 SQLAlchemy 中定义了一个 ORM(模型),如下所示:
class StoreView(Base):
__tablename__ = 'store_views'
id = Column(Integer, primary_key=True)
store_id = Column(Integer)
started_from = Column(TIMESTAMP)
end_to = Column(TIMESTAMP)
average_watch_time = Column(Float)
total_watch_time = Column(Float)
total_views = Column(Float)
我计划获取每天所有视图的总和,并尝试根据它们的 end_to 对结果进行分组。我在 sqlalchemy 中使用了以下查询:
result = session.query(
StoreView
).filter(
StoreView.started_from > from_date,
StoreView.end_to < to_date,
StoreView.store_id==5
).group_by( sa.func.year(StoreView.end_to), sa.func.month(StoreView.end_to)).all()
但是这个查询会抛出这个错误:
(psycopg2.errors.UndefinedFunction) function year(timestamp without time zone) does not exist
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
我在我的模型中使用时间戳,但由于某种原因我不打算更改它。我唯一能做的就是修改查询。 SQLAlchemy 已连接到 AWS Redshift。
【问题讨论】:
标签: python sql sqlalchemy