###わからないこと
いつもお世話になっております。
この度データのinsert処理をコーディングしているのですが、いまいち外部キー制約について理解できておらず、詰まっております。
insertを実行した結果、下記のようなエラーが出力されました。
###エラー
[Illuminate\Database\QueryException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`works`.`meta`, CONSTRAINT `cmeta_client_id_foreign` FOREIGN KEY (`client_id`) REFERE NCES `clients` (`id`)) (SQL: insert into `meta` (`item_id`, `value`, `created_at`, `updated_at`) values (3, ああああああ。, 2017-09-16 18:48:34, 2017-09-16 18:48 :34)) [PDOException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`works`.`meta`, CONSTRAINT `meta_client_id_foreign` FOREIGN KEY (`client_id`) REFERE NCES `clients` (`id`))
foreach ($client as $clientId) { $clientTbl->insert(['created_at' => $data, 'updated_at' => $data]); $metas = $mysql2->table('ct_meta')->select('item_id', 'value')->where('lient_id', $clientId)->get(); foreach ($metas as $meta) { $meta = (array)$meta; $metaTbl->insert(['item_id' => $meta['item_id'], 'value' => $meta['value'], 'created_at' => $data, 'updated_at' => $data]); }
こちらを解消するにはどのように変更すればよいでしょうか。
ご教授いただけないでしょうか。
よろしくお願いいたします。
※↑の情報では足りないと思い、追加でコードを記載しました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。