首页 > 解决方案 > 在 MySQL 上部署的 Django 给出 #1071 错误“指定的密钥太长”

问题描述

我已经尝试这个问题很长时间了。但我似乎无法找到解决方案。我正在尝试将我的项目部署在我选择 MySQL 的 pythonanywhere 上。每当我尝试使用 python manage.py 迁移我的模型时。我收到以下错误:

在此处输入图像描述

这是我的模型。

模型.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Thing(models.Model):
    thing_name = models.CharField(max_length=64, unique=True)

    def __str__(self):
        return self.hobby_name

class Person(models.Model):
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
    )
    first_name = models.CharField(max_length=64, null=True, blank=False)
    last_name = models.CharField(max_length=64, null=True, blank=False)
    hobbies = models.ManyToManyField(Hobby, blank=False)
    email = models.EmailField(max_length=64, blank=True, null=True, unique=True)
    city = models.CharField(max_length=64, null=True, blank=False)
    zip_code = models.CharField(max_length=10, null=True, blank=False)
    description = models.TextField(max_length=2048, null=True, blank=True)
    gender = models.CharField(max_length=1,
                       choices=(
                                ('N', 'No answer'),
                                ('M', 'Male'),
                                ('F', 'Female'),
                                ('O', 'Other')
                       ), null=True, blank=True, default="N"
                       )
    bad = models.BooleanField(default=False)
    good = models.BooleanField(default=True)
    maximum_hours = models.PositiveIntegerField(default=2)

    def __str__(self):
        try:
            string = self.first_name + " " + self.last_name
        except:
            string = "name_error"
        return string

最后要注意的是,我已经多次删除并重新创建了数据库。每次通过:

CREATE DATABASE database_name DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

我正在将所有这些部署到 Pythonanywhere。

模型 Thing 实际上变成了一张桌子。但是模型 Person 没有。

编辑:这是完整的追溯

(hobbyin) 01:16 ~/hobbyin (master)$ python manage.py migrate                                                                                                  
System check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It 
is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: account, admin, auth, contenttypes, external_page, sessions, sites, socialaccount
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying account.0001_initial... OK
  Applying account.0002_email_max_length... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying external_page.0001_initial...Traceback (most recent call last):
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    rowcount = self._do_query(q)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
    db.query(q)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 314, in create_model
    self.execute(sql, params or None)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
    cursor.execute(sql, params)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    rowcount = self._do_query(q)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
    db.query(q)
  File "/home/maxxie/.virtualenvs/hobbyin/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')

标签: pythonmysqldjangopythonanywhere

解决方案


找到了答案。当您将代码部署到生产环境时。确保您的迁移文件已删除。否则可能会导致诸如此类的意外错误。


推荐阅读