Repository navigation
AttributeError: 'asyncpg.pgproto.pgproto.UUID' object has no attribute 'replace' #712
Copy link
Copy link
Open
Labels
questionA community question, closed when inactive.A community question, closed when inactive.
Description
- GINO version: 1.0.1
- Python version: 3.8.2
- asyncpg version: 0.20.1
- PostgreSQL version: 12.3 (Ubuntu 12.3-1.pgdg20.04+1)
Description
I'm trying to use UUID value as unique Id in my model
from . import db
from uuid import uuid4
from sqlalchemy.dialects.postgresql import UUID
class User(db.Model):
__tablename__ = "users"
id = db.Column(UUID(as_uuid=True), primary_key=True, unique=True, index=True, nullable=False, default=uuid4)
login = db.Column(db.String(255), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=True)
full_name = db.Column(db.String(255))
last_login = db.Column(db.DateTime, nullable=True)
is_superuser = db.Column(db.Boolean, nullable=False, default=False)
is_staff = db.Column(db.Boolean, nullable=False, default=True)
remark = db.Column(db.String)
My controller is
class UserModel(BaseModel):
login: str
password: str
full_name: str
is_superuser: bool = False
is_staff: bool = True
remark: str = None
@router.post("/users")
async def add_user(user: UserModel):
rv = await User.create(login=user.login,
password=user.password,
full_name=user.full_name,
is_superuser=user.is_superuser,
is_staff=user.is_staff,
remark=user.remark
)
return rv.to_dict()
What I Did
When I'm trying to post a new user to db via swagger UI I got this error:
INFO: 127.0.0.1:38548 - "POST /users HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/petr/crm/.venv/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 386, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/fastapi/applications.py", line 181, in __call__
await super().__call__(scope, receive, send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/gino_starlette.py", line 79, in __call__
await self.app(scope, receive, send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/fastapi/routing.py", line 196, in app
raw_response = await run_endpoint_function(
File "/home/petr/crm/.venv/lib/python3.8/site-packages/fastapi/routing.py", line 147, in run_endpoint_function
return await dependant.call(**values)
File "./src/crm/views/users.py", line 30, in add_user
rv = await User.create(login=user.login,
File "/home/petr/crm/.venv/lib/python3.8/site-packages/gino/crud.py", line 444, in _create_without_instance
return await cls(**values)._create(bind=bind, timeout=timeout)
File "/home/petr/crm/.venv/lib/python3.8/site-packages/gino/crud.py", line 478, in _create
for k, v in row.items():
File "/home/petr/crm/.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 207, in items
return [(key, self[key]) for key in self.keys()]
File "/home/petr/crm/.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 207, in <listcomp>
return [(key, self[key]) for key in self.keys()]
File "/home/petr/crm/.venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1328, in process
value = _python_UUID(value)
File "/usr/lib/python3.8/uuid.py", line 166, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'asyncpg.pgproto.pgproto.UUID' object has no attribute 'replace'
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
questionA community question, closed when inactive.A community question, closed when inactive.
Activity
wwwjfy commentedon Aug 25, 2020
It doesn't need
as_uuid=Truebecause asyncpg already returns a UUID object as you see in the error messagexwr-ron commentedon Nov 12, 2023
Hello. I'm now encountering a similar error.
My table state:
A static method that, according to Telegram ID, returns an instance of the
Userclass:I replaced this kind of statement with the following:

This helped me, I hope it helps someone too
lyf2000 commentedon Oct 17, 2024
@wwwjfy hi! still dont get it why removing
as_uuid=Truesolves problem if it's default valuebut i encountered same problem:
after select() I still get
payment.uid.__class__as<class 'asyncpg.pgproto.pgproto.UUID'>, but not UUID from uuid pckgmb you have any idea, ty
PS
SQLAlchemy==2.0.34
asyncpg==0.29.0
psycopg2==2.9.9