いつもお世話になっています。
DBにデータを登録したいのですが、
以下のエラーが発生し登録ができません。
ご教示のほどお願いいたします。
UsersテーブルとGroupsテーブルが存在し、
Usersのgroup_idがGruopsのidに対してforeign keyを設定しています。
Groupsにid=1のデータは存在しています。
- エラーは①を行った際に発生しています。
Error: [PDOException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`cakephp`.`users`, CONSTRAINT `users_ibfk_2` FOREIGN KEY (`groups_id`) REFERENCES `groups` (`id`) ON DELETE CASCADE)
- src/Controller/IndexController.php
php
1$insert = [ 2 "id" => null, 3 "companys_id" => 1, 4 "groups_id" => 1, 5 "account_id" => "test", 6 "name" => "山田太郎", 7 "email" => "test@test.test", 8 "password" => $password, 9 "disk" => $disk, 10 "active" => 0, 11 "role" => 1, 12 "created" => $now, 13 "modified" => $now 14]; 15 16$trUsers = TableRegistry::get("Users"); 17$entity = $trUsers->newEntity(); 18$entity = $trUsers->patchEntity($entity, $insert); 19$result = $trUsers->save($entity); //①
- DB
>use cakephp; >select * from groups; +----+-------------+----------+---------------------+---------------------+ | id | companys_id | username | created | modified | +----+-------------+----------+---------------------+---------------------+ | 1 | 1 | group1 | 2016-06-27 12:07:56 | 2016-06-27 12:07:56 | +----+-------------+----------+---------------------+---------------------+
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。