gpt4 book ai didi

python - 您如何配置 Django 3 以与 MySql 5 一起使用?

转载 作者:行者123 更新时间:2023-12-03 23:49:30 26 4
gpt4 key购买 nike

我使用的是 Mac 10.13.6 和 MySql 5.5(目前无法升级)。我刚刚创建了一个框架 Django (v 3.0.2)/Python 3.7 应用程序并像这样配置了我的数据库设置

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'sql_mode': 'traditional',
},
'NAME': 'maps_data',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306',
}
}

我已经使用 pip 安装了 mysqlclient ...
(env) localhost:maps davea$ pip install mysqlclient
Requirement already satisfied: mysqlclient in /Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages (1.4.6)

但是,当我尝试运行为我创建的初始迁移时,我收到了这些我不明白的错误......
(env) localhost:maps davea$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/connections.py", line 239, 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)' at line 1")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 67, in ensure_schema
editor.create_model(self.Migration)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/connections.py", line 239, in query
_mysql.connection.query(self, query)
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)' at line 1")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 233, in handle
fake_initial=fake_initial,
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 91, in migrate
self.recorder.ensure_schema()
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 69, 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 ((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)' at line 1"))

要使用的 Django 和 mysqlclient 的正确版本是什么,或者我还需要做什么才能正确配置?

最佳答案

MySQL 5.5 在 Django 2.1 中被删除。这是changelog .
所以官方的答案是升级旧版本的 MySQL。
...也就是说,由于这个原因,我在生产中执行迁移失败,我能够使用以下过程解决它:

  • 获取迁移将使用的原始 sql python manage.py sqlmigrate myapp 0015
  • 其中“myapp”是您的应用程序的名称,“0015”是您尝试运行的迁移编号。

  • 根据需要调整查询。
  • 在这种情况下;将出现的 'datetime(6)' 替换为 'datetime'

  • 手动运行新查询。
  • 通过调用 python manage.py migrate myapp 0015 --fake 告诉 django 已手动运行迁移。
  • 关于python - 您如何配置 Django 3 以与 MySql 5 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922343/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com