question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
hrist0stoichevcommented, Jul 29, 2021

Finally made it work… I guess sqlalchemy is just not inferring the type correctly. Sorry for opening the issue.

from sqlalchemy.types import TIMESTAMP

sel = select([User.id, bindparam("timestamp", datetime.datetime.now(datetime.timezone.utc), type_=TIMESTAMP(timezone=True))])
await conn.execute(sel)
0reactions
jhomsmcommented, May 13, 2022

Should this then reported as a sqlalchemy issue?

Looks like SQLAlchemy is well aware of this, as documentation is quite explicit:

In sqlalchemy.types.DateTime

For timezone support, use at least the TIMESTAMP datatype, if not the dialect-specific datatype object.

Also, in sqlalchemy.types.TIMESTAMP

TIMESTAMP datatypes have support for timezone storage on some backends, such as PostgreSQL and Oracle. Use the TIMESTAMP.timezone argument in order to enable “TIMESTAMP WITH TIMEZONE” for these backends

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found