LoginSignup
0

posted at

Truncate時にCannot truncate a table referenced in a foreign key constraintとエラーが出た時の対処法

解決策

以下のように外部キー制約を一時的に外してtruncateする

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE table $table_name; 
SET FOREIGN_KEY_CHECKS = 1;

ちなみに、外部キー制約の状態は以下コマンドで確認可能

SELECT @@foreign_key_checks;
+----------------------+
| @@foreign_key_checks |
+----------------------+
|                    1 |
+----------------------+

参考

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
What you can do with signing up
0