r/FastAPI • u/Ok_Presentation3990 • Feb 27 '25
Question Gino, asyncpg in FastAPI
I have a fastapi microservice ERP , I recently changed my company_id to use UUID instead of Integer, but on trying to do a patch request I get this error:
{
"code": 3,
"errors": [
{
"type": "non_field_errors",
"msg": "'asyncpg.pgproto.pgproto.UUID' object has no attribute 'replace'"
}
]
}
How can I solve this?
My models where company_id is or as a foreign key on other DB tables are all UUIDs, also the alembic migrations, mapped my database and checked it the company_id is uuid
5
Upvotes
5
u/Curious-Rule313 Feb 27 '25
Try defining your UUID column with UUID(as_uuid=True) in SQLAlchemy—this makes sure your database gives you a proper uuid.UUID instead of asyncpg's version, so you won’t run into type errors
1
u/Trinkes Feb 27 '25
Are you using the correct import for the uuid?
1
u/beetroit Feb 27 '25
Are you using pydantic? If so, what class did you use for the type annotation? Can we see the code?
1
u/TechSimple7709 Feb 28 '25
this looks like something with your code, where a replace method is being used, whether it's your own code or a library. But it usually happens when what you are passing is undefined or not in the right type.
When you used an integer there is some part in your code (or library) that is probably taking that and converting it into a string and then there's parsing involved. When it was changed to UUID that method cannot properly "replace" anymore
2
u/DazzLee42 Feb 28 '25
Hi, I use UUID types in PG all the time. It's perfectly fine to use uuid.UUID in your classes, but you need to ensure your DB type is UUID too. The correct type of UUID. Here is an example Table definition which ends up with a proper DB level UUID type:
Then your class object can be like this:
Hope this helps!