Django MySQL error when running tests in a virtual

2019-08-27 03:44发布

问题:

I have a Django app working perfectly on my local Python 3.6 version and want to make sure that it will do so when installed elsewhere.

For this reason I created a virtualenv using precisely the same Python version which works fine globally, but without any packages:

virtualenv --no-site-packages --python=$(which python3.6) clear_env
source clear_env/bin/activate

Then I installed the requirements locally:

pip install -r requirements.txt 

When I try to run the server, or even when I used the admin panel and make changes to the DB, everything works. However, when I run the tests:

python manage.py test --nomigrations

I get the following error:

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL, `user_id` integer NOT NULL, `content_type_id` integer NULL, `objec' at line 1")

which tracebacks to:

  File "/home/niki/basic-django-ecommerce-site/clear_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "/home/niki/basic-django-ecommerce-site/clear_env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/home/niki/basic-django-ecommerce-site/clear_env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/home/niki/basic-django-ecommerce-site/clear_env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 312, in _query
    db.query(q)
  File "/home/niki/basic-django-ecommerce-site/clear_env/lib/python3.6/site-packages/MySQLdb/connections.py", line 224, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL, `user_id` integer NOT NULL, `content_type_id` integer NULL, `objec' at line 1")

Note: I am using the --nomigrations flag in the testing to avoid this issue. This again works perfectly well in my global Python environment.

Initially I thought that the issue may be related to some missing linux MySQL/Python packages, similar to that issue. I remember that once I had to install something like libmysqlclient-dev python-dev and recompile the Python version to get it working.

However, provided that my virutalenv is using the same Python version which works globally, what could be the reason? And even more strange, why only the tests fail with that error and the runserver and everything else related to DB is working?

回答1:

Solved. It was due to a possible bug in pipreqs, which I use to build requirements.txt. My global app version was using Django==2.0 and I use Mysql 5.5. For some reason, pipreqs has specified as a requirement Django==2.1.5 which supports only MySQL > 5.6, as stated here As a result, when doing the fresh requirements installation in the virtualenv, the newer Django version was installed, which was not communicating properly with my local MySQL version.