Skip to content
Advertisement

asyncpg.exceptions.DataError: invalid input for query argument $1: 217027642536 (value out of int32 range)

I’m working on a project that uses FastAPI alongside Pydantic and SQLAlchemy. I’m also using encode/databases to manage database connections. But for some weird reason, I get asyncpg.exceptions.DataError: invalid input for query argument $1: 217027642536 (value out of int32 range) anytime I try saving to the database. Here’s what my code looks like:

database.py

JavaScript

database_manager.py

JavaScript

endpoints.py

JavaScript

models.py

JavaScript

services.py

JavaScript

I can’t see anything wrong with this. Someone, please tell me what the hell is going on?

Advertisement

Answer

You are basically having Integer Overflow, Int32 represents 2^31 - 1, that means it can store the values in range -2147483648 to 2147483648 but the value you are trying to insert is bigger than 2^31

JavaScript

So you need to use SQLAlchemy’s BigInteger type represents Int64 it also represents 2^63 - 1 which can store the values in range negative and positive 9,223,372,036,854,775,807

JavaScript

Changing market’s Column Type with the BigInteger should solve the problem, but be careful bigger types uses more memory.

Advertisement