Skip to content

Commit

Permalink
Fixed #33507 -- Used UUID data type on MariaDB 10.7+.
Browse files Browse the repository at this point in the history
Co-Authored-By: Mariusz Felisiak <[email protected]>
  • Loading branch information
raydeal and felixxm committed Aug 2, 2023
1 parent ee36c33 commit 7cd187a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ answer newbie questions, and generally made Django that much better:
Akshesh Doshi <[email protected]>
[email protected]
Alasdair Nicol <https://al.sdair.co.uk/>
Albert Defler <[email protected]>
Albert Wang <https://github.com/albertyw/>
Alcides Fonseca
Aldian Fazrihady <[email protected]>
Expand Down
10 changes: 9 additions & 1 deletion django/db/backends/mysql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
# types, as strings. Column-type strings can contain format strings; they'll
# be interpolated against the values of Field.__dict__ before being output.
# If a column type is set to None, it won't be included in the output.
data_types = {

_data_types = {
"AutoField": "integer AUTO_INCREMENT",
"BigAutoField": "bigint AUTO_INCREMENT",
"BinaryField": "longblob",
Expand Down Expand Up @@ -133,6 +134,13 @@ class DatabaseWrapper(BaseDatabaseWrapper):
"UUIDField": "char(32)",
}

@cached_property
def data_types(self):
_data_types = self._data_types.copy()
if self.features.has_native_uuid_field:
_data_types["UUIDField"] = "uuid"
return _data_types

# For these data types:
# - MySQL < 8.0.13 doesn't accept default values and implicitly treats them
# as nullable
Expand Down
5 changes: 5 additions & 0 deletions django/db/backends/mysql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,8 @@ def supports_expression_defaults(self):
if self.connection.mysql_is_mariadb:
return True
return self.connection.mysql_version >= (8, 0, 13)

@cached_property
def has_native_uuid_field(self):
is_mariadb = self.connection.mysql_is_mariadb
return is_mariadb and self.connection.mysql_version >= (10, 7)
6 changes: 5 additions & 1 deletion django/db/backends/mysql/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

FieldInfo = namedtuple(
"FieldInfo",
BaseFieldInfo._fields + ("extra", "is_unsigned", "has_json_constraint", "comment"),
BaseFieldInfo._fields
+ ("extra", "is_unsigned", "has_json_constraint", "comment", "data_type"),
)
InfoLine = namedtuple(
"InfoLine",
Expand Down Expand Up @@ -62,6 +63,8 @@ def get_field_type(self, data_type, description):
return "PositiveIntegerField"
elif field_type == "SmallIntegerField":
return "PositiveSmallIntegerField"
if description.data_type.upper() == "UUID":
return "UUIDField"
# JSON data type is an alias for LONGTEXT in MariaDB, use check
# constraints clauses to introspect JSONField.
if description.has_json_constraint:
Expand Down Expand Up @@ -172,6 +175,7 @@ def to_int(i):
info.is_unsigned,
line[0] in json_constraints,
info.comment,
info.data_type,
)
)
return fields
Expand Down
5 changes: 4 additions & 1 deletion django/db/models/fields/related_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,10 @@ def get_prefetch_queryset(self, instances, queryset=None):
return (
queryset,
lambda result: tuple(
getattr(result, "_prefetch_related_val_%s" % f.attname)
f.get_db_prep_value(
getattr(result, f"_prefetch_related_val_{f.attname}"),
connection,
)
for f in fk.local_related_fields
),
lambda inst: tuple(
Expand Down
36 changes: 36 additions & 0 deletions docs/releases/5.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ Models
* :meth:`.QuerySet.aiterator` now supports previous calls to
``prefetch_related()``.

* On MariaDB 10.7+, ``UUIDField`` is now created as ``UUID`` column rather than
``CHAR(32)`` column. See the migration guide above for more details on
:ref:`migrating-uuidfield`.

Pagination
~~~~~~~~~~

Expand Down Expand Up @@ -483,6 +487,38 @@ Using ``create_defaults__exact`` may now be required with ``QuerySet.update_or_c
``create_defaults`` that are used with an ``update_or_create()`` should specify
the field in the lookup with ``create_defaults__exact``.

.. _migrating-uuidfield:

Migrating existing ``UUIDField`` on MariaDB 10.7+
-------------------------------------------------

On MariaDB 10.7+, ``UUIDField`` is now created as ``UUID`` column rather than
``CHAR(32)`` column. As a consequence, any ``UUIDField`` created in
Django < 5.0 should be replaced with a ``UUIDField`` subclass backed by
``CHAR(32)``::

class Char32UUIDField(models.UUIDField):
def db_type(self, connection):
return "char(32)"

For example::

class MyModel(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)

Should become::

class Char32UUIDField(models.UUIDField):
def db_type(self, connection):
return "char(32)"


class MyModel(models.Model):
uuid = Char32UUIDField(primary_key=True, default=uuid.uuid4)

Running the :djadmin:`makemigrations` command will generate a migration
containing a no-op ``AlterField`` operation.

Miscellaneous
-------------

Expand Down

0 comments on commit 7cd187a

Please sign in to comment.