没有不值得去解决的问题,也没有不值得去学习的技术!

Error When Adding a Foreign Key Constraint: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

An error occurred when adding the foreign key constraint: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails.

1. An error occurred when adding the foreign key constraint: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails. See Figure 1.

An error occurred when adding the foreign key constraint: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails.
Figure 1
Plaintext

PS E:\wwwroot\larabbs> php artisan migrate
Migrating: 2022_01_05_170327_add_references

   Illuminate\Database\QueryException

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`larabbs`.`#sql-13d0_5a1`, CONSTRAINT `topics_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: alter table `topics` add constraint `topics_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

  at E:\wwwroot\larabbs\vendor\laravel\framework\src\Illuminate\Database\Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) {
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         }
    707▕     }

  1   E:\wwwroot\larabbs\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
      PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`larabbs`.`#sql-13d0_5a1`, CONSTRAINT `topics_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE)")

  2   E:\wwwroot\larabbs\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
      PDOStatement::execute()
PS E:\wwwroot\larabbs>

2. After analyzing the cause, the issue occurs because a record with user ID 9 exists in the topics table, but no longer exists in the users table. See Figure 2.

After analyzing the cause, the issue occurs because a record with user ID 9 exists in the topics table, but no longer exists in the users table.
Figure 2

3. I finally decided to delete the record with user ID 9 from the topics table. Running the command again no longer produced an error, and the foreign key constraint topics_user_id_foreign was successfully added. See Figure 3.

I finally decided to delete the record with user ID 9 from the topics table. Running the command again no longer produced an error, and the foreign key constraint topics_user_id_foreign was successfully added.
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系