首页 > 解决方案 > 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

标签: pythonpostgresqlsqlalchemy

解决方案


推荐阅读