99from cdh .files .db .wrappers import FileWrapper
1010
1111
12+ USE_CHAR32_UUIDS = getattr (settings , "USE_CHAR32_UUIDS" , False ,)
1213logger = logging .getLogger ('cdh.files' )
1314
1415
@@ -29,14 +30,44 @@ def __set__(self, instance, value):
2930 instance ._file_wrapper_cache = value
3031
3132
33+ class MySQLSafeUUIDField (models .UUIDField ,):
34+ """
35+ Workaround class.
36+
37+ Django 5.0 switched from DB column type for UUID's on MySQL/MariaDB.
38+ The standard UUIDField is not aware of this change, and will fail to
39+ insert UUID's into the char(32) columns. For applications that run into
40+ this problem, this UUID field is provided which respects the setting
41+ USE_CHAR32_UUIDS in settings.py to support older fields.
42+
43+ Details: https://docs.djangoproject.com/en/6.0/releases/5.0/#migrating-existing-uuidfield-on-mariadb-10-7
44+ """
45+
46+ def __init__ (self , * args , ** kwargs ):
47+ self .use_char32_uuids = USE_CHAR32_UUIDS
48+ super ().__init__ (* args , ** kwargs )
49+
50+ def db_type (self , connection ):
51+ if self .use_char32_uuids :
52+ return "char(32)"
53+ return super ().db_type (connection )
54+
55+ def get_db_prep_value (self , value , connection , prepared = False ):
56+ value = super ().get_db_prep_value (value , connection , prepared )
57+ breakpoint ()
58+ if self .use_char32_uuids and value is not None :
59+ value = value .hex
60+ return value
61+
62+
3263class BaseFile (models .Model ):
3364 class Meta :
3465 abstract = True
3566
3667 objects = manager .FileManager ()
3768
3869 # Human-facing PK; Also acts as the filename on disk
39- uuid = models . UUIDField (
70+ uuid = MySQLSafeUUIDField (
4071 "Universally Unique IDentifier" ,
4172 unique = True ,
4273 default = uuid .uuid4 ,
0 commit comments