0
CREATE TABLE usager(
  nomUsager VARCHAR(20) PRIMARY KEY,
  motDePasse VARCHAR(20) NOT NULL
);

CREATE TABLE motCle(
  keyword VARCHAR(50) PRIMARY KEY
);

CREATE TABLE article(
  id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  titre VARCHAR(100),
  texte TEXT,
  nomUsager VARCHAR(20), 
  FOREIGN KEY (nomUsager) REFERENCES usager(nomUsager) 
);

CREATE TABLE article_motsCle(
  idArticle SMALLINT UNSIGNED,
  keyword VARCHAR(50),
  PRIMARY KEY (idArticle, keyword),
  FOREIGN KEY (idArticle) REFERENCES article(id),
  FOREIGN KEY (keyword) REFERENCES motCle(keyword)
);

all insert worked except the one into the article_motsCle table

INSERT INTO article_motsCle VALUES
(1, "neutron stars");

I am getting error #1452 - Cannot add or update a child row: a foreign key constraint fails.

Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
  • Use ctrl+K to format selected code text – Nikola Lukic Jun 02 '18 at 20:31
  • Possible duplicate of [ERROR 1452: Cannot add or update a child row: a foreign key constraint fails](https://stackoverflow.com/questions/21659691/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails) – Nikola Lukic Jun 02 '18 at 20:33
  • first create and entry in usager table, one in motCle table, then one in article (get the id of the row you created) and only last add one entry to article_motsCle table having the id of the row in article table – dtmiRRor Jun 02 '18 at 20:51

0 Answers0