python - SQLAlchemy 将 to_timestamp 函数的结果转换为日期
问题描述
我使用 Postgres 和 SQLAlchemy 作为 ORM。
我有这个查询,我似乎无法将其转换为 SQLAlchemy。
select to_timestamp(create_time)::date from inventory_item
create_time 列是纪元时间。这个查询的结果是这样的:
2019-02-24
这是我到目前为止尝试过的
InventoryItem.query.with_entities(db.func.to_timestamp(InventoryItem.create_time)).filter_by(inventory_id="1810092340894EA").first()
此查询的结果是这样的。
(datetime.datetime(2018, 10, 9, 23, 40, 17, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=480, name=None)),)
我只想要日期并排除时间。
我尝试了标记的重复答案,但它不起作用。
InventoryItem.query.with_entities(cast(InventoryItem.create_time, Date)).filter_by(order_id="1810092340894EA").first()
结果:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) cannot cast type integer to date
解决方案
推荐阅读
- javascript - 反应类组件。待办事项应用程序。如何使用 localStorage 存储数据
- flutter - 无法在警报对话框内容中加载资产
- sql - SQL 更新后触发器混淆
- vue.js - 断点未绑定,如何正确映射 webpack?
- c - bzero 没有按照它所说的去做
- javascript - 如何从异步函数获取返回值到 React Native 中的变量?
- pyspark - “DecisionTreeClassificationModel”对象没有属性“阶段”
- flutter - 如何在颤动中创建类似于 ToggleButton 的 TabBar?
- docker - Docker 容器正在运行但找不到它们
- javascript - 当函数工作时,else语句是执行的语句