#35032: Cannot save record with UUID field after migrating existing UUIDField
created in Django < 5.0
-------------------------------------+-------------------------------------
               Reporter:  Emanuel    |          Owner:  nobody
  Andrecut                           |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  5.0
  layer (models, ORM)                |       Keywords:  uuid, mariadb,
               Severity:  Normal     |  UUIDField, DataError
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Django 5.0 release notes specifies that on MariaDB 10.7+, UUIDField is now
 created as UUID column rather than CHAR(32) column and migrations where
 "char(32)" is hardcoded must be added to keep compatibility for
 UUIDField<s> created before version 5.0:
 https://docs.djangoproject.com/en/5.0/releases/5.0/#migrating-existing-
 uuidfield-on-mariadb-10-7

 However after applying such migrations as described in the release notes
 records cannot be saved anymore:

 {{{
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/query.py", line 953, in get_or_create
     return self.create(**params), True
            ^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/query.py", line 677, in create
     obj.save(force_insert=True, using=self.db)
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/base.py", line 814, in save
     self.save_base(
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/base.py", line 901, in save_base
     updated = self._save_table(
               ^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/base.py", line 1059, in _save_table
     results = self._do_insert(
               ^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/base.py", line 1100, in _do_insert
     return manager._insert(
            ^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/manager.py", line 87, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/query.py", line 1845, in _insert
     return query.get_compiler(using=using).execute_sql(returning_fields)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/models/sql/compiler.py", line 1822, in execute_sql
     cursor.execute(sql, params)
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/backends/utils.py", line 79, in execute
     return self._execute_with_wrappers(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
     return executor(sql, params, many, context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/backends/utils.py", line 100, in _execute
     with self.db.wrap_database_errors:
   File "/home/manu/venv/lib/python3.11/site-packages/django/db/utils.py",
 line 91, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/backends/utils.py", line 105, in _execute
     return self.cursor.execute(sql, params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-
 packages/django/db/backends/mysql/base.py", line 75, in execute
     return self.cursor.execute(query, args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-packages/MySQLdb/cursors.py",
 line 179, in execute
     res = self._query(mogrified_query)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/manu/venv/lib/python3.11/site-packages/MySQLdb/cursors.py",
 line 330, in _query
     db.query(q)
   File "/home/manu/venv/lib/python3.11/site-
 packages/MySQLdb/connections.py", line 255, in query
     _mysql.connection.query(self, query)
 django.db.utils.DataError: (1406, "Data too long for column 'id' at row
 1")
 }}}

 That is because, before this commit
 
https://github.com/django/django/commit/7cd187a5ba58d7769039f487faeb9a5a2ff05540,
 `has_native_uuid_field` would return False and after the 5.0 change,
 saving an  UUID for old fields that must still use CHAR(32) will not try
 to save it using the hex value of UUID:
 
https://github.com/django/django/blob/main/django/db/models/fields/__init__.py#L2737

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35032>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018c63acff50-1b06c8de-6fc1-4c40-9a4a-35e363154d3e-000000%40eu-central-1.amazonses.com.

Reply via email to