AttributeError: 'UUID' object has no attribute 'replace' when using backend-agnostic GUID type
This should fix it:
id = Column(GUID(as_uuid=True), ...)
from https://bitbucket.org/zzzeek/sqlalchemy/issues/3323/in-099-uuid-columns-are-broken-with:
"If you want to pass a
UUID()
object, theas_uuid
flag must be set to True."
The pg8000
PostgreSQL database adapter is returning a uuid.UUID()
object (see their type mapping documentation, and SQLAlchemy has passed that to the TypeDecorator.process_result_value()
method.
The implementation given in the documentation expected a string, however, so this fails:
>>> import uuid
>>> value = uuid.uuid4()
>>> uuid.UUID(value)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.7/uuid.py", line 133, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'
The quick work-around is to force the value to be a string anyway:
def process_result_value(self, value, dialect):
if value is None:
return value
else:
return uuid.UUID(str(value))
or you can test for the type first:
def process_result_value(self, value, dialect):
if value is None:
return value
else:
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value
I've submited pull request #403 to fix this in the documentation (since merged).