Search code examples
djangomariadbmysql-pythondjango-migrations

Issue When Running Django Migrations on MariaDB 10.5.8


I have been trying to migrate from a mysql database (version 5.7.26-29-31.37-log - output from SELECT VERSION()) to a mariadb (version 10.5.8-MariaDB-log - output from SELECT VERSION())

Also, I saw and updated django from version 2.2 to version 3.1.5, as mariadb is officially supported from version 3.0. With this, I also updated the mysqlclient library to 2.0.3.

But when on the server the "migrate" command is run, it fails with this error when doing it with the new DB: decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

Here is the stack-trace:

DEBUG (0.008) SELECT @@SQL_AUTO_IS_NULL; args=None
DEBUG (0.007) SHOW FULL TABLES; args=None
DEBUG (0.007) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
DEBUG (0.008) SHOW FULL TABLES; args=None
OUT Operations to perform:
OUT Apply all migrations: admin, auth, contenttypes, my_auth, essay, multichoice, quiz, sessions, sites, social_django, true_false
OUT Running pre-migrate handlers for application auth
OUT Running pre-migrate handlers for application contenttypes
OUT Running pre-migrate handlers for application sessions
OUT Running pre-migrate handlers for application sites
OUT Running pre-migrate handlers for application admin
OUT Running pre-migrate handlers for application quiz
OUT Running pre-migrate handlers for application multichoice
OUT Running pre-migrate handlers for application true_false
OUT Running pre-migrate handlers for application essay
OUT Running pre-migrate handlers for application my_auth
OUT Running pre-migrate handlers for application social_django
OUT Running migrations:
ERR [2021-01-06 09:32:31,188] (utils) DEBUG (0.007) SHOW FULL TABLES; args=None
ERR [2021-01-06 09:32:31,196] (utils) DEBUG (0.007) None; args=None
ERR Traceback (most recent call last):
ERR File "runapp.py", line 90, in <module>
ERR migrate()
ERR File "runapp.py", line 29, in migrate
ERR call_command("migrate", "-v 3", "--traceback")
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 168, in call_command
ERR return command.execute(*args, **defaults)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
ERR output = self.handle(*args, **options)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/base.py", line 85, in wrapped
ERR res = handle_func(*args, **kwargs)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 245, in handle
ERR fake_initial=fake_initial,
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 91, in migrate
ERR self.recorder.ensure_schema()
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 68, in ensure_schema
ERR editor.create_model(self.Migration)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 322, in create_model
ERR sql, params = self.table_sql(model)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 159, in table_sql
ERR definition, extra_params = self.column_sql(model, field)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 212, in column_sql
ERR db_params = field.db_parameters(connection=self.connection)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 718, in db_parameters
ERR check_string = self.db_check(connection)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 666, in db_check
ERR return connection.data_type_check_constraints[self.get_internal_type()] % data
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 344, in data_type_check_constraints
ERR if self.features.supports_column_check_constraints:
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/features.py", line 104, in supports_column_check_constraints
ERR if self.connection.mysql_is_mariadb:
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 372, in mysql_is_mariadb
ERR return 'mariadb' in self.mysql_server_info.lower()
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 360, in mysql_server_info
ERR cursor.execute('SELECT VERSION()')
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 98, in execute
ERR return super().execute(sql, params)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 66, in execute
ERR return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
ERR return executor(sql, params, many, context)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
ERR return self.cursor.execute(sql)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 73, in execute
ERR return self.cursor.execute(query, args)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
ERR res = self._query(query)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 321, in _query
ERR self._post_get_result()
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 355, in _post_get_result
ERR self._rows = self._fetch_row(0)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 328, in _fetch_row
ERR return self._result.fetch_row(size, self._fetch_type)
ERR decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

Does anybody know what the problem could be? To me it seems like the call to SELECT VERSION() triggers this conversion error.

Thanks!


Solution

  • In the end, to exchange the driver / db connector worked for me:

    1. Remove the mysqlclient from the dependencies

    2. Add mysql-connector-python~=8.0 to the dependencies (insert into requirements.txt)

    3. Adapt your DATABASES config like this in the settings.py file

      DATABASES = {
          'default': {
              'ENGINE': 'mysql.connector.django',
              'NAME': 'db_name',
              'USER': 'db_user',
              'PASSWORD': 'db_password',
              'HOST': 'db_host',
              'PORT': 3306,
              'OPTIONS': {
                      'autocommit': True,
                      'raise_on_warnings': True,
              },
          }
      }
      

    I hope this helps somebody!