Skip to content

Can't insert in postgres async dialect #7283

Closed
@ludaavics

Description

@ludaavics

Describe the bug

Inserting infinite value in a float column fails.
11

To Reproduce

import asyncio

from sqlalchemy import Column, Integer, Float
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine

Base = declarative_base()

async_engine = create_async_engine(
    "postgresql+asyncpg://user:password@localhost:5450/postgres",
    echo=True,
)

async_session = sessionmaker(async_engine, expire_on_commit=False, class_=AsyncSession)

class Test(Base):
    __tablename__ = "test"
    id = Column(Integer, primary_key=True)
    data = Column(Float)

async def async_main():
    async with async_engine.begin() as conn:
        await conn.run_sync(Base.metadata.drop_all)
        await conn.run_sync(Base.metadata.create_all)

    async with async_session() as session:
        test = Test(data=float("inf"))
        session.add(test)
        await session.commit()


if __name__ == "__main__":
    asyncio.run(async_main())

Error

sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $1: inf (numeric type does not support infinite values)
[SQL: INSERT INTO test (data) VALUES (%s) RETURNING test.id]
[parameters: (inf,)]

Versions

  • OS: ubuntu 20.04
  • Python: 3.6.10
  • SQLAlchemy: 1.4.26
  • Database: postgres 9.6.2
  • DBAPI (eg: psycopg, cx_oracle, mysqlclient): asyncpg

Additional context

Shameless copy/paste from MagicStack/asyncpg#811

Edit: correct link

Activity

zzzeek

zzzeek commented on Nov 2, 2021

@zzzeek
Member

i propose this is a driver issue. see #977. have you tried asyncpg directly?

added
external driver issuesthe issue involves a misbehavior on the part of the DBAPI itself, probably not SQLAlchemy
and removed
requires triageNew issue that requires categorization
on Nov 2, 2021
zzzeek

zzzeek commented on Nov 2, 2021

@zzzeek
Member

it's mentioned at MagicStack/asyncpg#811 and they state we'd use 'float" and not "numeric".

ludaavics

ludaavics commented on Nov 2, 2021

@ludaavics
Author

Plain asyncpg seems fine.
Numeric indeed do not support inf, but issue seems to be somewhere along the line asyncpg thinks the columns is numeric, not float?

import asyncio
import asyncpg

async def main():
    conn = await asyncpg.connect()
    try:
        await conn.execute('''
            DROP TABLE IF EXISTS test;
            CREATE TABLE test (
                id serial,
                data float
            );
        ''')

        await conn.execute('INSERT INTO test (data) VALUES ($1)', float('inf'));
        print(await conn.fetch('SELECT * FROM test'))
    finally:
        await conn.execute('DROP TABLE IF EXISTS test')
        await conn.close()

asyncio.run(main())
zzzeek

zzzeek commented on Nov 2, 2021

@zzzeek
Member

did you mean to link to MagicStack/asyncpg#811 ?

sqla-tester

sqla-tester commented on Nov 2, 2021

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

map Float to asyncpg.FLOAT, test for infinity https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3260

sqla-tester

sqla-tester commented on Nov 2, 2021

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the rel_1_4 branch:

map Float to asyncpg.FLOAT, test for infinity https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3261

ludaavics

ludaavics commented on Nov 2, 2021

@ludaavics
Author

Ah yes my mistake. Edited

added
bugSomething isn't working
datatypesthings to do with database types, like VARCHAR and others
and removed
external driver issuesthe issue involves a misbehavior on the part of the DBAPI itself, probably not SQLAlchemy
on Nov 2, 2021

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdatatypesthings to do with database types, like VARCHAR and otherspostgresql

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@ludaavics@sqla-tester

        Issue actions

          Can't insert in postgres async dialect · Issue #7283 · sqlalchemy/sqlalchemy