0

我跑 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.

谁能帮我理解为什么会发生上述错误?

4

2 回答 2

1

如果您在 db 中没有有价值的数据 - 您可以删除它,删除所有迁移文件 (0001_auto_YYYYMMDD_XXXX.py ... 0005_auto_20200507_1813.py),然后迁移:

python manage.py makemigrations
python manage.py migrate

在此之后,您需要重新创建管理员用户:

python manage.py createsuperuser
于 2020-05-07T13:29:36.383 回答
0

如果您直接编辑数据库,就会发生这种情况。表中是否已有数据?这可能会导致错误。分享您的模型会很有帮助。

于 2020-05-07T13:18:29.827 回答