Django 迁移时出现 MySQL 错误

3
我是一名有帮助的助手,以下是您需要翻译的内容:

我正在尝试按照Django网站上的“编写第一个Django应用程序”教程操作,但在有关数据库的第二部分中遇到了困难。

我正在运行Ubuntu 14.04和Python 3.4。

我已经安装了MySQL和Python MySQL库,并在MySQL中创建了一个名为testdb的数据库,但在尝试运行命令时

python3 manage.py migrate

I get the following error:

server1:~/Desktop/app/mysite$ python3 manage.py migrate
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 207, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 192, in execute
    r = self._query(query)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 355, in _query
    rowcount = self._do_query(q)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 319, in _do_query
    db.query(q)
_mysql_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 '%s' at line 1")

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

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 93, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 19, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 47, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 182, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 53, in ensure_schema
    editor.create_model(self.Migration)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 289, in create_model
    self.deferred_sql.extend(self._model_indexes_sql(model))
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/schema.py", line 55, in _model_indexes_sql
    self.connection.cursor(), model._meta.db_table
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/introspection.py", line 142, in get_storage_engine
"WHERE table_name = %s", [table_name])
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 207, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 192, in execute
    r = self._query(query)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 355, in _query
    rowcount = self._do_query(q)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 319, in _do_query
    db.query(q)
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 '%s' at line 1")

在Django中,我的数据库设置如下:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'testdb',
        'USER': 'root',
        'PASSWORD': 'xxxx',
        'HOST': '127.0.0.1',
    }
 }

我对基本的Django项目模板所做的唯一更改就是添加了一个应用程序,一些URL映射,并更改了数据库设置并将后端切换到SQLite,这运行良好。
任何帮助解决这个问题都将非常棒,谢谢。
编辑:这是我的已安装应用程序、URL和我还没有任何模型。
settings.py
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

项目 urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls'))
]

应用程序urls.py

urlpatterns = [url(r'^$', views.index, name='index')]

_mysql_exceptions.ProgrammingError: (1064, "您的SQL语法有误;请检查与您的MySQL服务器版本相对应的手册,以了解在第1行附近使用正确的语法") - 这为您提供了错误可能存在的提示。添加urls.pysettings.py中的INSTALLED_APPS部分和最终的models.py将会很有帮助。 - cezar
我添加了已安装的应用程序和URL,我的models.py文件完全为空。我知道mysql出了问题,但当mysql由Django生成时,我不知道从哪里开始查找。 - Mike Jones
所以,消息 "WHERE table_name = %s", [table_name]syntax to use near '%s' 让我想到了空表名的问题。如果 models.py 完全为空,并且你的 polls 应用程序不存在于 INSTALLED_APPS 中,那么你应该迁移什么呢?也许你应该使用 syncdb 创建一个基本的模式? - Max Vyaznikov
根据教程,它正在迁移一些已安装应用程序使用的表。我尝试注释掉所有已安装的应用程序,但仍然出现相同的错误,并且执行syncdb会出现类似于mysql语法的错误。 - Mike Jones
2个回答

2
问题似乎是由于MySQL连接器出了问题。在此帖子中找到了一个好的连接器。
不得不执行:
sudo apt-get install libmysqlclient-dev

那么

sudo pip3 install mysqlclient

1
如果使用wheel安装了mysqlclient,这在Windows上不能正常工作吗? - Apurva Kunkulol

0

首先,确保您已经删除了所有应用程序中的迁移文件

然后在每个应用程序中运行make migrations命令

python manage.py makemigrations <appname_1   # till appname_n>

那么

python manage.py migrate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接