Repository: django/django
File: django/db/models/functions/uuid.py (Python)

Code before:
    def as_sql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid4_function:
            return super().as_sql(compiler, connection, **extra_context)
        raise NotSupportedError("UUID4 is not supported on this database backend.")

    def as_postgresql(self, compiler, connection, **extra_context):
        if connection.features.is_postgresql_18:
            return self.as_sql(compiler, connection, **extra_context)
        return self.as_sql(
            compiler, connection, function="GEN_RANDOM_UUID", **extra_context
        )

    def as_mysql(self, compiler, connection, **extra_context):
        if not connection.features.supports_uuid4_function:
            if connection.mysql_is_mariadb:
                raise NotSupportedError("UUID4 requires MariaDB version 11.7 or later.")
            raise NotSupportedError("UUID4 is not supported on MySQL.")
        return self.as_sql(compiler, connection, function="UUID_V4", **extra_context)

    def as_oracle(self, compiler, connection, **extra_context):
        if not connection.features.supports_uuid4_function:
            raise NotSupportedError("UUID4 requires Oracle version 23ai or later.")
        return self.as_sql(compiler, connection, function="UUID", **extra_context)


class UUID7(Func):
    function = "UUIDV7"
    output_field = UUIDField()

    def as_sql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return super().as_sql(compiler, connection, **extra_context)
        raise NotSupportedError("UUID7 is not supported on this database backend.")

    def as_postgresql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return self.as_sql(compiler, connection, **extra_context)
        raise NotSupportedError("UUID7 requires PostgreSQL version 18 or later.")

    def as_sqlite(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return self.as_sql(compiler, connection, **extra_context)
        raise NotSupportedError(
            "UUID7 on sqlite requires Python version 3.14 or later."
        )

    def as_mysql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return self.as_sql(
                compiler, connection, function="UUID_V7", **extra_context
            )

Review comment (suggestion):
I know that based on the docs using `UUID` is OK, but what if we explicitly use the version number `UUID(4)`?

> UUID can optionally take as input a version_specifier of NUMBER type. UUID(0) and UUID(4) are equivalent to UUID() in that in both cases a version 4 variant 1 UUID is returned.
> Versions other than 4 and 0 return an error.

So, if in the future the `UUID` will change or be deprecated (like `GEN_RANDOM_UUID` in PG) we don't have to change nothing here.


Code after:

    def as_sql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid4_function:
            return super().as_sql(compiler, connection, **extra_context)
        raise NotSupportedError("UUID4 is not supported on this database backend.")

    def as_postgresql(self, compiler, connection, **extra_context):
        if connection.features.is_postgresql_18:
            return self.as_sql(compiler, connection, **extra_context)
        return self.as_sql(
            compiler, connection, function="GEN_RANDOM_UUID", **extra_context
        )

    def as_mysql(self, compiler, connection, **extra_context):
        if not connection.features.supports_uuid4_function:
            if connection.mysql_is_mariadb:
                raise NotSupportedError("UUID4 requires MariaDB version 11.7 or later.")
            raise NotSupportedError("UUID4 is not supported on MySQL.")
        return self.as_sql(compiler, connection, function="UUID_V4", **extra_context)

    def as_oracle(self, compiler, connection, **extra_context):
        if not connection.features.supports_uuid4_function:
            raise NotSupportedError(
                "UUID4 requires Oracle version 23ai/26ai (23.9) or later."
            )
        return self.as_sql(compiler, connection, function="UUID", **extra_context)


class UUID7(Func):
    function = "UUIDV7"
    arity = 0
    output_field = UUIDField()

    def as_sql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return super().as_sql(compiler, connection, **extra_context)
        raise NotSupportedError("UUID7 is not supported on this database backend.")

    def as_postgresql(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return self.as_sql(compiler, connection, **extra_context)
        raise NotSupportedError("UUID7 requires PostgreSQL version 18 or later.")

    # PY314: When dropping support for 3.14, remove the entire method.
    def as_sqlite(self, compiler, connection, **extra_context):
        if connection.features.supports_uuid7_function:
            return self.as_sql(compiler, connection, **extra_context)
        raise NotSupportedError(
            "UUID7 on SQLite requires Python version 3.14 or later."
        )