python - 将模型迁移到现有 MySQL 数据库时出现 Django 错误

标签 python mysql django django-contenttypes mysql-connector-python

背景:

在完成 Django“投票”教程后,我正在尝试使用 Django 和现有的 MySQL 数据库构建一个支持多对多关系的数据库。

我正在运行一个包含 Python 3.4、Django 1.9.2、mysql-connector-python 2.1.3 的虚拟环境。

我正在通过项目 settings.py 中的 'ENGINE': 'mysql.connector.django' 连接到在 MAMP 上运行的 MySQL 数据库

我的项目中的设置应该是正确的,因为我使用 inspect db 从现有的 MySQL 数据库中提取模型没有问题。

问题:

查看并编辑 inspectdb 创建的模型后,我想将它们迁移到数据库中。我将模型复制到应用程序的 models.py 并运行迁移命令。不幸的是,它现在抛出错误,并且是 django 的新手,我不太确定问题出在哪里。我看到了:

(开始错误摘录)

Operations to perform:
  Apply all migrations: admin, sessions, auth, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "/path/to/virtualenv/lib/python3.4/site-packages/mysql/connector/django/base.py", line 177, in _execute_wrapper
    return method(query, args)
  File "/path/to/virtualenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 515, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/path/to/virtualenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 434, in _handle_result
    self._handle_noresultset(result)
  File "/path/to/virtualenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 404, in _handle_noresultset
    self._warnings[0][1], self._warnings[0][2])
mysql.connector.errors.DatabaseError: 1264: Out of range value for column 'applied' at row 1

During handling of the above exception, another exception occurred:

(结束错误摘录)

我认为正在发生的事情

阅读该错误消息后,我将两件事之一视为罪魁祸首:内容类型或 mysql 连接器设置('ENGINE':'mysql.connector.django',)。

我找到了 ContentTypes 模型的 django 文档,但页面上的内容似乎无法帮助诊断我的问题。

我很好奇它是否是连接器的问题,如果是,为什么 inspectdb 在提取模型时没有问题。

谁能告诉我发生了什么事?

谢谢!

最佳答案

ATM 'mysql.connector.django' 似乎提供了对 Django 版本 <= 1.8 的支持

即使设置 USE_TZ = True 也会导致奇怪的类型错误,例如:

"TypeError: can't multiply sequence by non-int of type 'tuple'"

使用 MySQLdbmysqlclient(它是 MySQLdb 的分支)应该可以解决问题。

关于python - 将模型迁移到现有 MySQL 数据库时出现 Django 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740129/

相关文章:

python 帮助!如果/否则语句

php - 在 mysql 表中找到 "problematic"行将无法导出

php - iOS 上的 MySQL 编码问题

mysql - sql查询更新多列1

django - 当前路径,与其中任何一个都不匹配

python - 模块未找到错误: No module named 'mysite' when try to call django-admin

python - 尽管 django 中没有显示 key ,但 key 仍然存在

python - Flask-WTForms使用jQuery移至下一个表单字段

python - 训练的 Tensorflow CNN 回归 MSE 高于测试

python - Foursquare API 请求在版本参数上返回错误