【问题标题】:Django MySQL error when running tests in a virtualenv在 virtualenv 中运行测试时出现 Django MySQL 错误
【发布时间】:2019-07-22 16:27:19
【问题描述】:

我有一个 Django 应用程序可以在我的本地 Python 3.6 版本上完美运行,并希望确保它在安装到其他地方时也能正常运行。

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

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 manage.py migrate 它运行没有问题吗?似乎您的问题实际上是当您不使用 --nomigrations 标志时
  • @dan-klasson 在这种情况下抱怨没有数据库。如果我将数据库留在那里但将其清空,则会失败:django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax;...。即使--nomigrations 也存在问题。我还是觉得和virtualenv中的python/mysql包有关,我只安装了mysqlclient,但是还需要别的吗?

标签: python mysql django virtualenv django-testing


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多