Skip to content

Can't subtract offset-naive and offset-aware datetimes using bindparam #791

@hrist0stoichev

Description

@hrist0stoichev
  • 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions