我正在尝试为 Django 中的模型迁移一个简单的外键关系,但这会导致一个非常奇怪且无法解决的错误。
错误:
python manage.py migrate team
Operations to perform:
Apply all migrations: team
Running migrations:
Applying team.0001_initial... OK
Applying team.0002_auto_20191127_1643...Traceback (most recent call last):
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsutils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsmysqlbase.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbcursors.py", line 255, in execute
self.errorhandler(self, exc, value)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbconnections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbcursors.py", line 252, in execute
res = self._query(query)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbcursors.py", line 378, in _query
db.query(q)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbconnections.py", line 280, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1005, "Can't create table 'asp052mysqlpy.#sql-6ac_56' (errno: 150)")
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 "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangocoremanagementbase.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangocoremanagementcommandsmigrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbmigrationsmigration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbmigrationsoperationsfields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsbaseschema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsbaseschema.py", line 744, in _alter_field
self.execute(self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s"))
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsbaseschema.py", line 133, in execute
cursor.execute(sql, params)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsutils.py", line 100, in execute
return super().execute(sql, params)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsutils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsutils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsutils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbutils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsutils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesdjangodbbackendsmysqlbase.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbcursors.py", line 255, in execute
self.errorhandler(self, exc, value)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbconnections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbcursors.py", line 252, in execute
res = self._query(query)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbcursors.py", line 378, in _query
db.query(q)
File "C:UsersMitchDesktopteamworkteamworkapplibsite-packagesMySQLdbconnections.py", line 280, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1005, "Can't create table 'asp052mysqlpy.#sql-6ac_56' (errno: 150)")
模型:
class Tmteams(models.Model):
teamid = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
class Meta:
db_table = 'tmteams'
class Tmteamsusers(models.Model):
teamid = models.ForeignKey(Tmteams, on_delete=models.CASCADE, db_column='teamid')
userid = models.ForeignKey(User, on_delete=models.CASCADE, db_column='userid')
class Meta:
db_table = 'tmteamsusers'
unique_together = (('userid', 'teamid'),)
出错的迁移文件:
class Migration(migrations.Migration):
dependencies = [
('team', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='tmteamsusers',
name='teamid',
field=models.ForeignKey(db_column='teamid', on_delete=django.db.models.deletion.CASCADE, to='team.Tmteams'),
),
migrations.AlterField(
model_name='tmteamsusers',
name='userid',
field=models.ForeignKey(db_column='userid', on_delete=django.db.models.deletion.CASCADE, to='users.User'),
),
]
我正在使用以下模块和MySQL版本: 姜戈 2.1.5 MysqlClient 1.3.14 MySQl 5.1.7.3 社区版
有人可以帮助我吗?
我已经找到并解决了这个问题。我引用的表(allmodusers(仍在使用MyISAM数据库引擎。在阅读以下文章后,我已将此表的数据库引擎更改为InnoDB:https://kinsta.com/knowledgebase/convert-myisam-to-innodb/。