Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating relations: #1452 - Cannot add or update a child row: a foreign key constraint fails
    primarykey
    data
    text
    <p>I've tried to create a new table relation but I seem to have gotten an error for which I'm not sure why. The first relation (one to many) I made was last week and it works good, maybe I forgot the correct procedure since then?</p> <p>Simple explanation: I have three tables - events, music_styles, venues</p> <p>So far, I correctly linked events and venues in the way that one venue can have multiple events linked to it. I tried making a new table that has rows for each musical style and an ID that would be used in table 'events' so that I can link each event to a musical style id.</p> <p>However this is the error I got:</p> <blockquote> <p>SQL query:</p> <p>ALTER TABLE <code>events</code> ADD FOREIGN KEY ( <code>MUSIC_STYLE_ID</code> ) REFERENCES <code>nightl7_complete</code>.<code>music_styles</code> ( <code>MUSIC_ID</code> ) ON DELETE NO ACTION ON UPDATE NO ACTION ;</p> <p>MySQL said: Documentation</p> <h1>1452 - Cannot add or update a child row: a foreign key constraint</h1> <p>fails (<code>nightl7_complete/#sql-2721_c0dcfd</code>, CONSTRAINT <code>#sql-2721_c0dcfd_ibfk_2</code> FOREIGN KEY (<code>MUSIC_STYLE_ID</code>) REFERENCES <code>music_styles</code> (<code>MUSIC_ID</code>) ON DELETE NO ACTION ON UPDATE NO ACTION) Documentation Error ALTER TABLE <code>events</code> ADD FOREIGN KEY ( <code>MUSIC_STYLE_ID</code> ) REFERENCES <code>nightl7_complete</code>.<code>music_styles</code> ( <code>MUSIC_ID</code> ) ON DELETE NO ACTION ON UPDATE NO ACTION ;</p> </blockquote> <p>And this is the procedure I did :)</p> <p><img src="https://i.stack.imgur.com/l91iN.jpg" alt="enter image description here"></p> <p><a href="https://i.imgur.com/d5L62.jpg" rel="nofollow noreferrer">I uploaded a full sized image here</a> </p> <p>Thanks everyone :)</p> <hr> <p>Edit:</p> <p>So I did this:</p> <pre><code>$query = 'SELECT e.* FROM events e '. 'LEFT JOIN nightl7_complete.music_styles ms ON ms.ID = e.MUSIC_STYLE_ID'. 'WHERE ms.id IS NULL'; $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } </code></pre> <p>and got this:</p> <p>Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ms.id IS NULL' at line 1</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.
    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