Skip to content

Instantly share code, notes, and snippets.

@gmolveau
Last active January 17, 2025 02:56
Show Gist options
  • Select an option

  • Save gmolveau/7caeeefe637679005a7bb9ae1b5e421e to your computer and use it in GitHub Desktop.

Select an option

Save gmolveau/7caeeefe637679005a7bb9ae1b5e421e to your computer and use it in GitHub Desktop.
sqlalchemy uuid for sqlite
########################
# UUID for SQLite hack #
########################
from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects.postgresql import UUID
import uuid
class GUID(TypeDecorator):
"""Platform-independent GUID type.
Uses PostgreSQL's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""
impl = CHAR
def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
return dialect.type_descriptor(UUID())
else:
return dialect.type_descriptor(CHAR(32))
def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == 'postgresql':
return str(value)
else:
if not isinstance(value, uuid.UUID):
return "%.32x" % uuid.UUID(value).int
else:
# hexstring
return "%.32x" % value.int
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
class Base(db.Model):
__abstract__ = True
id = db.Column(GUID(), primary_key=True, default=lambda: str(uuid.uuid4()))
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime,
default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp())
class User(Base):
__tablename__ = 'users'
username = db.Column(db.String, nullable=False, unique=True)
email = db.Column(db.String, nullable=False, unique=True)
encrypted_password = db.Column(db.String, nullable=False)
def set_password(self, password):
self.encrypted_password = bc.generate_password_hash(password)
def verify_password(self, password):
return bc.check_password_hash(self.encrypted_password, password)
@RobertoPrevato

Copy link
Copy Markdown

Thanks!!

@Toshinaki

Toshinaki commented Jan 29, 2022

Copy link
Copy Markdown

Great gist!

Is server_default=DefaultClause(xxx)) a better choice when handling id, created_at and updated_at?

And there's a warning:

SAWarning: TypeDecorator GUID() will not produce a cache key because the ``cache_ok`` attribute is not set to True.  
This can have significant performance implications including some performance degradations in comparison to prior 
SQLAlchemy versions.  
Set this attribute to True if this type object's state is safe to use in a cache key, or False to disable this warning.

@TomerHeber

TomerHeber commented May 6, 2022

Copy link
Copy Markdown

This almost works.
Missing sort_key_function

This is my version:

class GUID(TypeDecorator):
    """Platform-independent GUID type.
    Uses PostgreSQL's UUID type, otherwise uses
    CHAR(32), storing as stringified hex values.
    """

    impl = CHAR
    cache_ok = True

    def load_dialect_impl(self, dialect):
        if dialect.name == "postgresql":
            return dialect.type_descriptor(UUID())
        else:
            return dialect.type_descriptor(CHAR(32))

    def process_bind_param(self, value, dialect):
        if value is None:
            return value
        elif dialect.name == "postgresql":
            return str(value)
        else:
            if not isinstance(value, uuid.UUID):
                return "%.32x" % uuid.UUID(value).int
            else:
                # hexstring
                return "%.32x" % value.int
    
    def _uuid_value(self, value):
        if value is None:
            return value
        else:
            if not isinstance(value, uuid.UUID):
                value = uuid.UUID(value)
            return value         

    def process_result_value(self, value, dialect):
        return self._uuid_value(value)

    def sort_key_function(self, value):
        return self._uuid_value(value)

@splint3rsec

Copy link
Copy Markdown

Awesome! Also you might consider changing the name of process_result_value to something else. :)

@tinynakji

Copy link
Copy Markdown

Thank you, this was useful!

@bfontaine

Copy link
Copy Markdown

@splint3rsec: why so? This is needed by SQLAlchemy.

@thomas1989

Copy link
Copy Markdown

crazy creative workaround. thx for your work!

@wafflecomposite

Copy link
Copy Markdown

Still so useful it's a second link in a google for "fastapi UUID sqlite" query for me. Thanks!

@FinchPowers

Copy link
Copy Markdown

I've found sqlalchemy.types.Uuid to be working.

It does the clever thing, use native UUID support when available, use string when it's not.

@faisalPdev

Copy link
Copy Markdown

Thank you , its worked. In my case UUID is working in PostgreSQL, but in SQLite for testing, I struggled a lot. I tried all the methods, and none of them worked except this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment