我跑 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.
谁能帮我理解为什么会发生上述错误?