代码之家  ›  专栏  ›  技术社区  ›  Alien13

在mysql上部署django会给出“指定的密钥太长”错误

  •  0
  • Alien13  · 技术社区  · 6 年前

    我已经尝试这个问题很长时间了。但我似乎找不到解决办法。我正试图在pythonanywhere上部署我的项目,在那里我选择了mysql。每当我尝试使用python manage.py migrate迁移模型时。我得到以下错误:

    enter image description here

    这是我的模特。

    模特儿

    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;
    

    我要把这一切都部署到皮托那。

    模型实际上变成了一张桌子。但模范人物没有。

    编辑:这是完整的回溯

    (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 回复  |  直到 6 年前
        1
  •  0
  •   Alien13    6 年前

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