5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails 〜みたいなエラーの解決

Last updated at Posted at 2019-10-25

環境

NodeJS
MySQL

状況

別環境へのデータの復元的な処理を作ってて、
自動的にいろんなテーブルにinsertするような仕組みにしようとした中で出たエラーです。

エラー内容

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (db_dd.tenant, CONSTRAINT <font color="DeepPink">dataFileRelation</font> FOREIGN KEY (code, nm) REFERENCES <font color="DeepPink">hogeFile</font>  (code, e_nm))    0.000 sec

解決方法

今回はhogeFileテーブルのカラムの外部キーがdataRelationテーブルにあるので、先にdataFileRelationテーブルをinsertしてしまうとhogeFileテーブルで参照できなくなり、不整合なデータとなってエラーとなるので、先にhogeFileテーブルのレコードを先にinsertする必要がある。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?