当我运行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.
任何人都可以帮助我理解为什么发生上述错误吗?
答案 0 :(得分:0)
如果数据库中没有有价值的数据-您可以删除它,删除所有迁移文件(0001_auto_YYYYMMDD_XXXX.py ... 0005_auto_20200507_1813.py),然后迁移:
python manage.py makemigrations
python manage.py migrate
您需要在此之后重新创建管理员用户:
python manage.py createsuperuser