MagicStack / MagicStack/asyncpg

Invalid OID Handling in SQLAlchemy with asyncpg and autoload_with Causes DataError

Open
#1,212 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

I encountered an issue when using SQLAlchemy with asyncpg and the autoload_with option while trying to load table metadata. The problem occurs because PostgreSQL's oid type is treated as a signed 32-bit integer (int32) by asyncpg, which leads to a DataError when the OID value exceeds the maximum range of int32 (2,147,483,647).

### Steps to Reproduce
1. Create a PostgreSQL table with an OID that exceeds the int32 range. For example, an OID like 3195477613.
2. Use SQLAlchemy to define the table with autoload_with to load metadata:

```python
from sqlalchemy import Table, MetaData

metadata = MetaData()
table = Table("master_product_view", metadata, autoload_with=conn)
```

3. Execute the code.

### Observed Behavior
The following exception is raised during metadata loading:

```python
sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) : invalid input for query argument $1: 3195477613 (value out of int32 range)
```

### Expected Behavior
The autoload_with option should correctly handle OID values, even if they exceed the int32 range.

### asyncpg/SQLAlchemy Version in Use

asyncpg 0.30.0
sqlalchemy 1.4.54

### Database Vendor and Major Version

PostgreSQL 14

### Python Version

3.13

### Operating system

Linux

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing the SQLAlchemy Table(..., autoload_with=conn) metadata load against PostgreSQL 14 with asyncpg 0.30.0 and the reported OID, then trace the OID parameter handling. Done means metadata loading succeeds for OIDs above the int32 range without DataError.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python, sqlalchemy
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.