Django + MySQL - Admin Site - Add User - OperationalError - SAVEPOINT does not exist

不羁的心 提交于 2019-12-06 06:12:59

This bug haunted me for a long time, so I decided to dig further and try to resolve it once and for all.

Root Cause: The SAVEPOINT issue is a bug that occurs only in MySQL-Python connector.

Fix: Use other MySQL drivers for Python (e.g. mysqlclient).

Details/Findings:

  • Tried the MySQL binaries in Homebrew, MAMP, and XAMPP for Mac.
  • Tried various MySQL versions, 5.6 (libmysqlclient.18.dylib) and 5.7 (libmysqlclient.20.dylib).
  • Tried various Python's MySQL drivers.

No relationships found by varying the MySQL binaries/versions. But I've narrowed down the issue by testing various MySQL drivers commonly used in Python:

  1. MySQLdb (widely-used but old database connector, last commit was 7 years ago!):

    $ pip install MySQL-python

  2. mysqlclient (modern version of MySQL-python, but w/ lots of bug fixes and improvements):

    $ pip install mysqlclient

  3. PyMySQL (pure Python MySQL database driver):

    $ pip install PyMySQL

    Then, add in settings.py (just below the import os):

    try:
        import pymysql
        pymysql.install_as_MySQLdb()
    except:
        pass
    
  4. MySQL-Connector-Python by Oracle (pure Python MySQL database driver):

    $ pip install mysql-connector-python-rf

    Then, edit the database's ENGINE configuration in settings.py:

    'ENGINE': 'mysql.connector.django',
    

The SAVEPOINT issue occurs only when using the MySQL-python connector (#1 driver), but not in the others (#2, #3, #4 drivers). On my case, I had chosen the mysqlclient. Issue is gone now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!