将latin1_swedish_ci更改为utf8_general_ci后,无法迁移db

时间:2017-06-01 08:12:06

标签: mysql django migrate python-social-auth

昨天我删除了我的数据库表,因为我需要将latin1_swedish_ci更改为utf8_general_ci,以便在我的应用中使用波兰语。之后我无法完全迁移我的数据库,因为我有这样的问题:

    (venv) C:\netbast\fastandbeauty\project>manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, comments, contenttypes, registration, sessions, social_django, user_content, user_profile
Running migrations:
  Applying social_django.0005_auto_20160727_2333...Traceback (most recent call last):
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\connections.py", line 292, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1071, 'Specified key was too long; max key length is 1000 bytes')

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

Traceback (most recent call last):
  File "C:\netbast\fastandbeauty\project\manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\migrations\operations\models.py", line 536, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\base\schema.py", line 353, in alter_unique_together
    self.execute(self._create_unique_sql(model, columns))
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\base\schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\netbast\fastandbeauty\venv\lib\site-packages\MySQLdb\connections.py", line 292, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 1000 bytes')

我不知道我能做什么,因为我是学徒。我找到了答案,但它对我没有帮助。

抱歉我的英语不好。

PS。当我启动服务器它运行,但社交django不起作用。只有该应用无法迁移。

一切顺利, 鼎晟

2 个答案:

答案 0 :(得分:0)

免责声明:我不是这方面的专家,我只是陈述我目前的做法,这里的建议只是一个建议最终你决定了如果你要使用它。

让我们尝试解决问题,在您的终端中输入:

  • mysql -u root
  • 然后在您输入mysql监视器后,通过键入fastandbeauty
  • 检查您的SHOW_DATABASE;数据库是否仍然存在
  • 如果数据库仍在那里,请将其删除DROP DATABASE fastandbeauty;
  • 然后通过键入CREATE SCHEMA fastandbeauty DEFAULT CHARACTER SET utf8重新创建并创建一个新的全新数据库,如果一切正常,监视器应该省略消息Query OK, 1 row affected (0.00 sec),之后您可以再次键入SHOW_DATABASE并查看是否数据库存在
  • 键入mysql monitor
  • 离开\q
  • 然后你可以python manage.py migrate

应该创建一个新的fastandbeauty database,您可以继续使用它。

现在有一些未来的参考:

我建议您不要在本地制作中使用开发数据库,​​这样就不会遇到需要手动创建/删除数据库的事情。

因此,您可以将dump数据放入文件中,使用该文件,您可以存储一些数据用于本地开发。如何使用它?

你可以创建新的git branch并命名为local-dev-task-12,这样你将来可以在这个分支上工作,然后你就可以只为这个分支创建新的数据库并调用数据库{ {1}},只需执行我已经准备好的步骤,但在fast_and_beauty12migration之前,请执行您保留cd的文件夹,然后执行此操作:

  • dump file - 这会将您的数据转储到新的mysql -u root -p fast_and_beauty_12 < DumpFastAndBeauty.sql已创建的数据库
  • 进入您的fast_and_beauty_12文件并将数据库名称更改为您创建的新名称,以便setting db&gt;&gt; fastandbeauty所以你可以使用它
  • 然后你去做fast_and_beauty12

通过这个,您将拥有一个新的python manage migrate.py数据库,您可以在不担心对git branch数据库造成伤害的情况下处理您的数据库。

注:

您不会从该分支提交新的dev文件,因为这会使事情变得糟糕,您将git stash。他们将来的工作。

在您隐藏migration个文件后,请返回migrations并将您的setting.py名称更改为local database,以便development database改为fast_and_beauty12 1}},将新更改迁移到fastandbeauty,然后您可以

development branch,因此应该使用新数据填充python manage.py migrate

这样做的想法是每次为本地开发创建新分支时都有新的工作数据库,一开始有点混乱,但经过一段时间后你会理解它,并开始更频繁地开始。如果你不打算在你的数据库上工作,你可能不需要这样做,但我认为这是一个很好的做法。

答案 1 :(得分:0)

我现在没有问题。你能做些什么来解决它?

您需要转到my.ini(SQL中的conf文件)并更改MYISAM

default_tmp_storage_engine=MYISAM

default-storage-engine=InnoDB

感谢您的帮助!