MagicStack / MagicStack/asyncpg
invalid input for query argument (can't subtract offset-naive and offset-aware datetimes)
未关闭
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 8.1k
- 派生
- 468
- PR 合并指标
- 30 天内没有已合并 PR
描述
- asyncpg version: 0.29.0
- PostgreSQL version: 13.8
- Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?: local postgres 13.8-alpine running in docker - Python version: 3.12.1
- Platform: macos
- Do you use pgbouncer?: no
- Did you install asyncpg with pip?: yes
- If you built asyncpg locally, which version of Cython did you use?: N/A
- Can the issue be reproduced under both asyncio and
uvloop?: N/A
I'm getting an error:
File "asyncpg/protocol/prepared_stmt.pyx", line 204, in asyncpg.protocol.protocol.PreparedStatementState._encode_bind_msg
asyncpg.exceptions.DataError: invalid input for query argument $1: datetime.datetime(2024, 3, 21, 21, 17, 9... (can't subtract offset-naive and offset-aware datetimes)
When passing python's datetime object with timezone details to be stored in a column without timezone. Minimal steps to reproduce:
import asyncio
import datetime
import asyncpg
DSN = 'postgresql://user:password@host:port/database'
CREATE_SQL = """
DROP TABLE IF EXISTS timestamps_test;
CREATE TABLE timestamps_test
(
ts_no_tz timestamp without time zone,
ts_tz timestamp with time zone
);
"""
INSERT_SQL = "INSERT INTO timestamps_test (ts_no_tz, ts_tz) VALUES ($1, $2);"
async def main():
conn = await asyncpg.connect(DSN)
try:
await conn.execute(CREATE_SQL)
now = datetime.datetime.fromisoformat("2024-03-21 21:17:09.631169+07:00")
await conn.execute(INSERT_SQL, now, now)
finally:
await conn.close()
if __name__ == '__main__':
asyncio.run(main())
I understand that timezone details would be lost either way, but it's inconsistent with PostgreSQL's native behaviour, when trying to insert the same date with psql:
$ psql
psql (15.6, server 13.8)
Type "help" for help.
INSERT INTO
timestamps_test (ts_no_tz, ts_tz)
VALUES
(TIMESTAMP '2024-03-21 21:17:09.631169+07:00',
TIMESTAMP WITH TIME ZONE '2024-03-21 21:17:09.631169+07:00');
INSERT 0 1
# SELECT * FROM timestamps_test;
ts_no_tz | ts_tz
----------------------------+-------------------------------
2024-03-21 21:17:09.631169 | 2024-03-21 14:17:09.631169+00
(1 row)
timezone info is ignored and lost but there's no error.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
检查 asyncpg/protocol/prepared_stmt.pyx 的第 204 行以及 timestamp 参数的编码路径;首先运行随附的 Python/PostgreSQL 复现程序。当 aware datetime 可以传递给 timestamp without time zone 且不会产生 offset-mismatch error,同时 timestamp with time zone 情况保持现有行为时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- postgresql, python
- 领域
- databases
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100