在我的Django项目中,我试图将数据库字段从OneToOne更改为ForeignKey。当我运行“ python manage.py migration”时,它以长回溯中断,结尾是我在标题中引用的警告。我知道其他人也遇到了这个问题,但是我找不到能解决这个问题的解决方案。
我已将以下内容添加到我的local_settings.py数据库中:
'OPTIONS': {
'autocommit': True,
'charset': 'utf8mb4',
}
起初,我尝试将OneToOne更改为ForeignKey,但是遇到了这个问题。因此,我决定通过先删除该字段,然后再将其添加为新的字段类型来分步执行。但是我什至不能删除该字段。
这是makemigrations生成的迁移文件,是迁移中断的文件:
# Generated by Django 2.2.3 on 2019-07-18 09:59
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('sweaters', '0011_auto_20190718_0954'),
]
operations = [
migrations.RemoveField(
model_name='sweater',
name='yarn_for',
),
]
这是我得到的完整追溯:
$ python manage.py migrate
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, sessions, sweaters
Running migrations:
Applying sweaters.0012_remove_sweater_yarn_for...Traceback (most recent call last):
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/utils.py", line 96, in inner
return func(*args, **kwargs)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/mysql/connector/cursor.py", line 897, in fetchall
self._handle_eof(eof)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/mysql/connector/cursor.py", line 838, in _handle_eof
self._warnings[0][1], self._warnings[0][2])
mysql.connector.errors.DatabaseError: 3719: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
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 "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/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 "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/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 "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 178, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 479, in remove_field
fk_names = self._constraint_names(model, [field.column], foreign_key=True)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 1115, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/mysql/connector/django/introspection.py", line 320, in get_constraints
for constraint, column, ref_table, ref_column in cursor.fetchall():
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/utils.py", line 96, in inner
return func(*args, **kwargs)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/django/db/utils.py", line 96, in inner
return func(*args, **kwargs)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/mysql/connector/cursor.py", line 897, in fetchall
self._handle_eof(eof)
File "/Users/frankjernigan/Documents/webdev/phrancko-web-dev/phrancko.com/phrancko-project/myenv/lib/python3.7/site-packages/mysql/connector/cursor.py", line 838, in _handle_eof
self._warnings[0][1], self._warnings[0][2])
django.db.utils.DatabaseError: (3719, "3719: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.", None)
答案 0 :(得分:0)
这是MySQL切换为默认设置且StandardDefinition
尚未更新的问题。这里提供了一种解决方法:
https://code.djangoproject.com/ticket/29678
您使用的mysql-connector-python
是哪个版本?我想知道它是否可能已在更新版本中通过链接中的解决方法进行了修补。