MagicStack / MagicStack/asyncpg
invalid input for query argument (can't subtract offset-naive and offset-aware datetimes)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 8.1k
- Fork
- 468
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
* **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](https://github.com/magicstack/uvloop)?**: N/A
I'm getting an error:
```shell
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:
```python
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
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.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Kiểm tra asyncpg/protocol/prepared_stmt.pyx tại dòng 204 và đường đi mã hóa đối số timestamp; bắt đầu bằng cách chạy bản tái hiện Python/PostgreSQL được cung cấp. Công việc được xem là hoàn tất khi một aware datetime có thể được truyền vào timestamp without time zone mà không gây ra offset-mismatch error, trong khi trường hợp timestamp with time zone vẫn giữ nguyên hành vi hiện tại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- postgresql, python
- Lĩnh vực
- databases
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 35/100