当我运行 makemigrations
完全没有问题。但当试图运行 migrate
,出现以下错误。
django.db.utils.IntegrityError: The row in table 'main_tutorial' with primary key
'1' has an invalid foreign key: main_tutorial.tutorial_series_id contains a value '1'
that does not have a corresponding value in main_tutorialseries.id.
这是我的 makemigrations
输出。
> python manage.py makemigrations
Migrations for 'main':
main\migrations\0005_auto_20200507_1813.py
- Create model TutorialCategory
- Add field tutorial_slug to tutorial
- Alter field tutorial_published on tutorial
- Create model TutorialSeries
- Add field tutorial_series to tutorial
而当我运行 python manage.py migrate
:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, main, sessions
Running migrations:
Applying main.0005_auto_20200507_1813...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\core\management\commands\migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_i
nitial=fake_initial)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\db\migrations\executor.py", line 247, in apply_migration
migration_recorded = True
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\db\backends\sqlite3\schema.py", line 35, in __exit__
self.connection.check_constraints()
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\django\db\backends\sqlite3\base.py", line 326, in check_constraints
raise utils.IntegrityError(
django.db.utils.IntegrityError: The row in table 'main_tutorial' with primary ke
y '1' has an invalid foreign key: main_tutorial.tutorial_series_id contains a va
lue '1' that does not have a corresponding value in main_tutorialseries.id.
谁能帮我理解为什么会出现上述错误?
如果你直接编辑数据库,就会出现这种情况.表中是否已经有数据,这可能会导致错误。分享你的模型会很有帮助。
如果你没有有价值的数据在db - 你可以删除它,删除所有的迁移文件(0001_auto_YYYYMMDD_XXXX.py......0005_auto_20200507_1813.py),然后迁移。
python manage.py makemigrations
python manage.py migrate
之后你需要重新创建管理员用户。
python manage.py createsuperuser