在virtualenv中运行测试时出现Django MySQL错误

问题描述 投票:0回答:1

我有一个Django应用程序完全在我的本地Python 3.6版本上工作,并希望确保它将安装在其他地方时这样做。

出于这个原因,我使用完全相同的Python版本创建了一个virtualenv,它在全局范围内运行良好,但没有任何包:

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

然后我在本地安装了这些要求:

pip install -r requirements.txt 

当我尝试运行服务器时,或者甚至当我使用管理面板并对数据库进行更改时,一切正常。但是,当我运行测试时:

python manage.py test --nomigrations

我收到以下错误:

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")

追溯到:

  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")

注意:我在测试中使用--nomigrations标志以避免this issue。这在我的全局Python环境中再次运行良好。

最初我认为这个问题可能与一些缺少的Linux MySQL / Python软件包similar to that issue有关。我记得有一次我必须安装类似libmysqlclient-dev python-dev的东西并重新编译Python版本才能使它工作。

但是,如果我的virutalenv使用的是同一个全球有效的Python版本,那可能是什么原因呢?甚至更奇怪的是,为什么只有测试失败并且错误和runserver以及与DB相关的所有其他工作?

python mysql django virtualenv django-testing
1个回答
0
投票

解决了。这是由于pipreqs中可能存在的错误,我用它来构建requirements.txt。我的全球应用程序版本使用Django==2.0,我使用Mysql 5.5。出于某种原因,pipreqs指定Django==2.1.5只支持MySQL> 5.6,如here所述因此,当在virtualenv中进行新的需求安装时,安装了较新的Django版本,这与我的本地没有正确通信MySQL版本。

© www.soinside.com 2019 - 2024. All rights reserved.