Reputation: 804
I'm building a fastAPI application. I have a Psotgres DB where i write and read data from. I'm using SQLAlchemy to interact with my Postgres Database. Here is my model:
import uuid
from db.database import Base
from sqlalchemy import Column, String, Boolean, DateTime, func
from sqlalchemy.dialects.postgresql import UUID
class User(Base):
__tablename__ = 'user'
id = Column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
unique=True,
nullable=False,
)
full_name = Column(String, nullable=True)
password = Column(String, nullable=True)
email = Column(String, unique=True, nullable=False)
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), nullable=True)
def __repr__(self):
return f'<User {self.email}>'
UserTable = User.__table__
And I'm trying to run a query like this:
from models.user import User as UserDB
import uuid
from db.database import database
from datetime import datetime
from asyncpg.exceptions import UniqueViolationError
from typing import Optional
async def get_user_by_email(email: str) -> Optional[UserDB]:
query = UserTable.select().where(UserTable.c.email == email)
try:
user_record = await database.fetch_one(query)
if user_record:
user_dict = dict(user_record)
return UserDB(**user_dict)
except Exception as e:
print(f"Error: {e}")
return None
This result in the below error:
Error: 'asyncpg.pgproto.pgproto.UUID' object has no attribute 'replace'
Upvotes: 0
Views: 221
Reputation: 804
Turns out as_uuid=True was not needed https://github.com/python-gino/gino/issues/712
Upvotes: 0