Can't subtract offset-naive and offset-aware datetimes using bindparam
See original GitHub issue- asyncpg version: 0.23.0
- PostgreSQL version: postgres:11-alpine
- Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?: I’m using docker image on a local machine
- Python version: 3.7.5
- Platform: mac
- Do you use pgbouncer?: no
- Did you install asyncpg with pip?: yes
- If you built asyncpg locally, which version of Cython did you use?:
- Can the issue be reproduced under both asyncio and uvloop?: I guess the event loop doesn’t really matter here
I have the following issue where I try to use bindparam to include a datetime with tzinfo in a select statement and then use it to insert in another table. This is a simplified version of the code (tested that it actually produces the error):
sel = select([User.id, bindparam("timestamp", datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc))])
ins = insert(DeletedUser).from_select([DeletedUser.id, DeletedUser.timestamp], sel)
await conn.execute(ins)
which results in
sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $1: datetime.datetime(2019, 5, 16, 2, 49, 55... (can't subtract offset-naive and offset-aware datetimes)
DeletedUser.timestamp is of type timestamp with timezone info which I manually verified.
The following code works without any issues:
await conn.execute(insert(DeletedUser).values(id=5, timestamp=datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)))
Am I doing something wrong?
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Can't subtract offset-naive and offset-aware datetimes
The correct solution is to add the timezone info e.g., to get the current time as an aware datetime object in ...
Read more >PYTHON : Can't subtract offset-naive and offset-aware datetimes
PYTHON : Can't subtract offset - naive and offset - aware datetimes [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] ...
Read more >Can't subtract offset-naive and offset-aware datetimes
I have a timezone aware timestamptz field in PostgreSQL. When I pull data from the table, I then want to subtract the time...
Read more >TypeError: can't subtract offset-naive and offset-aware datetimes
It meanse one of the datetime instance contains timezone info, while the others doens't. import datetime d1 = datetime.datetime.now(datetime.
Read more >TypeError: can't subtract offset-naive and ... - Google Groups
In poll app, I set Time_Zone='Asia/Kolkata' and USE_TZ=True in settings.py file. In models.py file,. import datetime. from django.utils import timezone.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Finally made it work… I guess sqlalchemy is just not inferring the type correctly. Sorry for opening the issue.
Looks like SQLAlchemy is well aware of this, as documentation is quite explicit:
In sqlalchemy.types.DateTime
Also, in sqlalchemy.types.TIMESTAMP