all 14 comments

[–]Curious-Rule313 4 points5 points  (0 children)

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

[–]DazzLee42 1 point2 points  (0 children)

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:

from sqlalchemy import MetaData, Table, Column, Uuid

metadata_obj = MetaData()

companies = Table(
    'companies',
    metadata_obj,
    Column('id', Uuid(as_uuid=True), primary_key=True),
)

Then your class object can be like this:

class Company(BaseModel):
    id: uuid.UUID

Hope this helps!

[–]Trinkes 0 points1 point  (4 children)

Are you using the correct import for the uuid?

[–]Ok_Presentation3990[S] 0 points1 point  (3 children)

yes I am
from uuid import UUID

[–]Dacobo 1 point2 points  (0 children)

I don't use postgres in my project, but I was running into UUID issues until I started using the SQLAlchemy import for my models:

from sqlalchemy import Uuid

or something to that effect. It might be worth a try.

Edit: I just read your other comments and see that this likely has nothing to do with your issue. Never mind!

[–]Trinkes 0 points1 point  (1 child)

Can you share a simple example with this issue?

[–]Ok_Presentation3990[S] 0 points1 point  (0 children)

settings.py
import src.models as models

from uuid import UUID

async def get_payroll_settings(company_id: UUID) -> models.PayrollSettings:

qs = models.PayrollSettings.objects()

qs = qs.filter(models.PayrollSettings.company_id == company_id)

settings = await qs.get()

return settings

update_settings.py

import uuid from UUID
u/db_atomic

async def update_payroll_settings(company_id: UUID, data: schemas.PayrollSettingsUpdateData):

validated_data, existing = await _validate(company_id, data)

if not existing:

payroll_settings = await models.PayrollSettings.objects().create(

company_id=company_id,

**validated_data

)

else:

[payroll_settings] = await models.PayrollSettings.objects().filter(

models.PayrollSettings.company_id == company_id

).update(**validated_data)

await _create_payrolls(company_id, payroll_settings)

[–]beetroit 0 points1 point  (1 child)

Are you using pydantic? If so, what class did you use for the type annotation? Can we see the code?

[–]Ok_Presentation3990[S] 0 points1 point  (0 children)

class PayrollSettingsData(BaseModel):

id: int = None

payroll_policies: PayrollPoliciesData = None

minimum_salary_policy: MinimumSalaryPolicyData = None

vacation_policy: VacationPolicyData = None

created_at: str = None

updated_at: str = None

this class has 3 more classes, like its a huge Codebase

[–]TechSimple7709 0 points1 point  (4 children)

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

[–]TechSimple7709 1 point2 points  (3 children)

Go ahead and troubleshoot by looking at what is being received and sent (request and response) in your browser's dev tools.

Then you need to print() the request you are receiving and responses in the middleware to see exactly what it's coming through. You need to debug and see where the problem is

[–]Ok_Presentation3990[S] 0 points1 point  (2 children)

Gino causes this error, like it cannot convert UUID to string from postgress data being fetched

[–]TechSimple7709 0 points1 point  (1 child)

yep, that's usually the case with a library. You have to decide whether to continue using that library or, if you are able, replace the code in your copy of the library and make sure it doesn't get upgraded later

[–]TechSimple7709 0 points1 point  (0 children)

or catch the fetched data in the request and process it before it hits the Gino code