Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Even though binary was set false, impl remain binary(16) #718

Open
BhuwanPandey opened this issue Oct 10, 2023 · 0 comments
Open

Even though binary was set false, impl remain binary(16) #718

BhuwanPandey opened this issue Oct 10, 2023 · 0 comments

Comments

@BhuwanPandey
Copy link

BhuwanPandey commented Oct 10, 2023

class UUIDType(ScalarCoercible, types.TypeDecorator):
"""
Stores a UUID in the database natively when it can and falls back to
a BINARY(16) or a CHAR(32) when it can't.

::

    from sqlalchemy_utils import UUIDType
    import uuid

    class User(Base):
        __tablename__ = 'user'

        # Pass `binary=False` to fallback to CHAR instead of BINARY
        id = sa.Column(
            UUIDType(binary=False),
            primary_key=True,
            default=uuid.uuid4
        )
"""

// value for impl remain same on binary = False

impl = types.BINARY(16)

python_type = uuid.UUID

cache_ok = True

def __init__(self, binary=True, native=True):
    """
    :param binary: Whether to use a BINARY(16) or CHAR(32) fallback.
    """
    self.binary = binary
    self.native = native

def __repr__(self):
    return util.generic_repr(self)

def load_dialect_impl(self, dialect):

    if self.native and dialect.name in ('postgresql', 'cockroachdb'):
        # Use the native UUID type.
        return dialect.type_descriptor(postgresql.UUID())

    if dialect.name == 'mssql' and self.native:
        # Use the native UNIQUEIDENTIFIER type.
        return dialect.type_descriptor(mssql.UNIQUEIDENTIFIER())

    else:
        # Fallback to either a BINARY or a CHAR.
        kind = self.impl if self.binary else types.CHAR(32)
        return dialect.type_descriptor(kind)
@BhuwanPandey BhuwanPandey changed the title Even though binary set false, impl remain as binary(16) Even though binary was set false, impl remain as binary(16) Oct 10, 2023
@BhuwanPandey BhuwanPandey changed the title Even though binary was set false, impl remain as binary(16) Even though binary was set false, impl remain binary(16) Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant