python - SQLAlchemy select by custom column -


i have sqlalchemy model custom id in uuid format

class email(base):     __tablename__ = 'email'      id = column(uuid(), primary_key=true, default=uuid.uuid4)     raw_email = column(text) 

and when try object id

session.query(email).get("0c7a2a97-c93b-4f26-9408-25d2bf597bc0") 

it returns error

can't escape uuid binary 

i understand problem in custom id because id uuid type, can create preprocessor or similar thing gives me opportunity select objects using str. because convert str uuid every time need select object annoying

i create own function converting

def str_to_uuid(value):     uuid = uuid()     return uuid.process_bind_param(value) 

and helpful if can decorate id use function.

or it's bad style ?

i understand why have problem this.

class uuid(types.typedecorator):     impl = types.largebinary      def __init__(self):         self.impl.length = 16         types.typedecorator.__init__(self, length=self.impl.length)      def process_bind_param(self, value, dialect=none):         if value , isinstance(value, uuid.uuid):             return value.bytes         elif value , isinstance(value, str):             return uuid.uuid(value)         elif value:             raise valueerror('value %s not valid uuid.uuid' % value)         else:             return none      def process_result_value(self, value, dialect=none):         if value:             return uuid.uuid(bytes=value)         else:             return none      def is_mutable(self):         return false 

this implementation of uuid column type. , mistake in process_bind_param. forgot add bytes

elif value , isinstance(value, str):             return uuid.uuid(value) 

so fix problem when change method to

def process_bind_param(self, value, dialect=none):         ...         elif value , isinstance(value, str):             return uuid.uuid(value).bytes         elif value:             raise valueerror('value %s not valid uuid.uuid' % value)         else:             return none 

maybe helpful someone.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -