Django deploying on MySQL gives #1071 error “Specified key was too long”

穿精又带淫゛_ 提交于 2019-12-13 03:24:56

问题


I have been trying this problem for a very long time. But I just cannot seem to find the solution. I am trying to deploy my project on pythonanywhere, where I have chosen MySQL. Whenever I try to migrate my models with python manage.py migrate. I get the following error:

This is my models.

models.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

Last note is that I have DROPPED and re-CREATED the database several times. Each time by:

CREATE DATABASE database_name DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

I am deploying all of this to Pythonanywhere.

The model Thing actually becomes a table. But the model Person does not.

Edit: Here is the full traceback

(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')

回答1:


Found the answer. When you deploy your code to production. Make sure that your migration files are removed. Otherwise it might cause unexpected errors such as this one.



来源:https://stackoverflow.com/questions/51334487/django-deploying-on-mysql-gives-1071-error-specified-key-was-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!