Django Migration Error 'TypeError: sequence item 1: expected a bytes-like object, str found' on mysql-connector-python cursor_cent.py file

Viewed 465

I'm using mysql-connector to handle mysql requests in a Django project. Problem is I'm setting up a simple project with "django-admin startproject project ." and when I try to do a simple manage.py migrate, this is my output.

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
aqui <mysql.connector.cursor_cext._ParamSubstitutor object at 0x7f408ae1ec10> b'SELECT engine FROM information_schema.tables WHERE table_name = %s'
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 91, in migrate
    self.recorder.ensure_schema()
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 68, in ensure_schema
    editor.create_model(self.Migration)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 348, in create_model
    self.deferred_sql.extend(self._model_indexes_sql(model))
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 1087, in _model_indexes_sql
    output.extend(self._field_indexes_sql(model, field))
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 1106, in _field_indexes_sql
    if self._field_should_be_indexed(model, field):
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/mysql/schema.py", line 111, in _field_should_be_indexed
    storage = self.connection.introspection.get_storage_engine(
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/mysql/connector/django/introspection.py", line 270, in get_storage_engine
    cursor.execute(
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/mysql/connector/django/base.py", line 149, in execute
    return self.cursor.execute(query, new_args)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/mysql/connector/cursor_cext.py", line 265, in execute
    stmt = RE_PY_PARAM.sub(psub, stmt)
TypeError: sequence item 1: expected a bytes-like object, str found

I have the settings already properly configured as

DATABASES = {
    'default': {
        'NAME': 'dbname',
        'ENGINE': 'mysql.connector.django',
        'USER': 'XXXXX',
        'PASSWORD': 'XXXXX',
        'OPTIONS': {
            'autocommit': True
        }
    }
}

and the dbname database properly created via mysql shell. I also notice that two tables are created on this db when I run "python manage.py migrate" 2 times (the first time I run it, the above error happens but no tables are created on my db).

I tried messing with code near stmt = RE_PY_PARAM.sub(psub, stmt), even tried to input stmt.decode('utf-8'). It then raises a different error at the same point

tmt = RE_PY_PARAM.sub(psub, stmt.decode('utf-8'))
TypeError: cannot use a bytes pattern on a string-like object

I am using Ubuntu 20.04 and am doing all of this in a python venv, with django and mysql-connector-python installed via pip.

2 Answers

This appears to be a bug with mysql-connector-python==8.0.27 and can be avoided by rolling back one version:

pip install mysql-connector-python<=8.0.26

Do you have created and defined the two mandatory files views.py and urls.py for your app?

Related