66from sqlalchemy .orm import declared_attr , relationship
77from sqlalchemy .types import CHAR , TypeDecorator
88
9+ from tests .common import sqla_uri
10+
911
1012class Base :
1113 @declared_attr
@@ -214,9 +216,21 @@ def __repr__(self):
214216 return f"{ self .__class__ .__name__ } (id={ self .id } , name={ self .name !r} , user_id={ self .user_id } )"
215217
216218
217- class UUIDType (TypeDecorator ):
219+ class CustomUUIDType (TypeDecorator ):
218220 impl = CHAR
219221
222+ def __init__ (self , * args , as_uuid = True , ** kwargs ):
223+ """
224+ Construct a UUID type.
225+
226+ # TODO: support as_uuid=False (and set by default!)
227+ :param as_uuid=True: if True, values will be interpreted
228+ as Python uuid objects, converting to/from string via theDBAPI.
229+
230+ """
231+ super ().__init__ (* args , ** kwargs )
232+ self .as_uuid = as_uuid
233+
220234 def load_dialect_impl (self , dialect ):
221235 return CHAR (32 )
222236
@@ -230,6 +244,21 @@ def process_bind_param(self, value, dialect):
230244 def process_result_value (self , value , dialect ):
231245 return UUID (value )
232246
247+ @property
248+ def python_type (self ):
249+ return UUID if self .as_uuid else str
250+
251+
252+ db_uri = sqla_uri ()
253+ if "postgres" in db_uri :
254+ # noinspection PyPep8Naming
255+ from sqlalchemy .dialects .postgresql import UUID as UUIDType
256+ elif "sqlite" in db_uri :
257+ UUIDType = CustomUUIDType
258+ else :
259+ msg = "unsupported dialect (custom uuid?)"
260+ raise ValueError (msg )
261+
233262
234263class IdCast (Base ):
235264 id = Column (UUIDType , primary_key = True )
0 commit comments