python - Python 3 和 SQLite3 的 `table "django_migrations "already exists` 错误

标签 python django python-3.x sqlite python-3.6

我最近开始收到一个错误,无法识别使用 SQLite 的 Django 项目。下面链接的项目以前与 Python 2/3 兼容,尽管我主要使用 Python 2 运行它。最近,我将大部分计算机和项目切换为默认使用 Python 3(我知道,晚总比不好,对吧?)。此外,我将项目从 Django 1.7 升级到 1.11。此后,项目开始收到表已存在错误,但使用python3运行migrate命令时出现.

我也在运行python3 manage.py migrate时收到错误。例如,python3 manage.py test 工作得很好,但考虑到测试首先运行迁移,这有点令人困惑。运行 python2 manage.py migrate 工作正常,没有错误。

我正在 Mac 上运行 Python 3.6.4,通过 Homebrew 安装,收到的错误是:

File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 59, in ensure_schema
   raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (table "django_migrations" already exists)

我在多台计算机上遇到了完全相同的问题(使用 Python 3 时出现问题,但使用 Python 2 时出现问题),所以我想其他人也会看到同样的问题。您应该能够通过以下步骤重现该问题:

  1. git clone git@github.com:alexdlaird/django-bootstrap-authentication-template-project.git && cd django-bootstrap-authentication-template-project
  2. 进行安装
  3. python2 manage.py migrate - 请注意,它工作得很好
  4. rm db.sqlite 重新开始
  5. python3 manage.py migrate - 请注意,它失败并出现上面显示的错误 5x。 rm db.sqlite 重新开始 5x。 python3 manage.py test - 请注意,它工作得很好

要查看针对 MySQL 实例运行的迁移(该项目假设您有一个本地运行的默认 Homebrew MySQL 实例,但如果没有,可以在 .env 中配置),请运行 python3管理.py migrate 并观察到这在 Python 2 或 3 中运行得很好,因此该问题似乎与 SQLite 迁移无关。

有什么想法吗?我在这里做错了什么?

-- .pyc 更新 --

我已经执行了 find 。 -name\*.pyc -delete 在运行上述命令之间,但这并不能缓解这种情况。这是有道理的,因为问题在新的克隆上仍然存在,但我认为我们可以消除它是 Python 缓存文件的问题。

最佳答案

终于找到了。对于其他可能遇到类似问题的人,这里是我的项目中的违规行,我只需删除它们:https://github.com/alexdlaird/django-bootstrap-authentication-template-project/commit/db16ff88d0d6c25eed38e52bd8332c721ed21e2f?diff=split#diff-e398a065684e871bec35f76ea80f20a7

这是一个用于 Python 2.5/6 和 SQLite 的古老黑客的产物(参见 here )——这绝不是一个好的解决方案,但考虑到 SQLite 仅用于开发和测试,它工作得很好。这肯定比我连续几个小时因 Python 2/SQLite unicode 不兼容性而苦恼要好。

毫不奇怪,我很久以前就忘记了这个黑客攻击,直到我调试 Django 代码(在 the comment above 中描述)并意识到为什么字符串表名称具有丑陋的字节包装器时,我才想到这一点。

关于python - Python 3 和 SQLite3 的 `table "django_migrations "already exists` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503235/

相关文章:

python - codecademy python 练习 5.Pull it Together - 如何对行程成本求和()

python - import _tkinter # 如果失败,你的 Python 可能没有在 python 3.8 中配置 Tk 错误

mysql - SQLAlchemy 0.9.4 过滤组

python - 霍夫圆中的参数代表什么以及如何知道使用什么值?

python - django 中的 style.css 文件位置

python - 将字典中最大的 2 个键和值放入另一个字典中

Django 似乎从静态文件 URL 中剥离了 https——为什么?

python - TypeError : int() argument must be a string, 类似字节的对象或数字,而不是 'list'

python - 如何在 Matplotlib 中生成嵌套图例

python - Django ORM : Get latest record for distinct field