Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to Run a stored procedure in mySQL and it fails on a foreign key constraint
    primarykey
    data
    text
    <p>I wrote a simple Stored Procedure with a while loop that's not working. Here's some data for you: the parent table is data_client_id. The tables are all innoDB. The primary key is ID. All other tables in the schema with the data_ prefix have this (data_client_id.id) column as a foreign key. I generated some dummy data and inserted it into the data_client_id table which auto-increments the primary key. The rows generated were 33 through 132. My intention was to write one stored procedure that would create rows with matching values in the id column for all of the other data_ tables.</p> <p>Here is my code:</p> <pre><code>mysql&gt; DELIMITER // mysql&gt; CREATE PROCEDURE this() -&gt; BEGIN -&gt; DECLARE v1 INT; -&gt; SET v1 = 33; -&gt; WHILE v1 &lt; 132 DO -&gt; INSERT INTO data_banking (id) VALUES(v1); -&gt; SET v1 = v1+1; -&gt; END WHILE; -&gt; END// Query OK, 0 rows affected (0.00 sec) </code></pre> <p>Then when I CALL the this(); Procedure I get the following error:</p> <pre><code>mysql&gt; call this(); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`oms_clients`.`data_banking`, CONSTRAINT `data_banking_FK` FOREIGN KEY (`id`) REFERENCES `data_client_id` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) </code></pre> <p>I tried just running standalone INSERTs into the data_ tables like this: INSERT into data_banking (id) VALUES (34); </p> <p>and it worked setting all the columns with defaults to NULL or their respective SET defaults...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload