Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

sql - #1452 - Cannot add or update a child row: a foreign key constraint fails

When I wan't to connect two tables with eachother then I get the message : #1452 - Cannot add or update a child row: a foreign key constraint fails....I want to connect oauth_uid2 (primary key) from the facebook_users tabel to the bugs tabel with the foreign key oauth_uid2. But I get always this message. I have already cleared my data but nothing works.Also in the table users_facebook I have 1 record.

1452 - Cannot add or update a child row: a foreign key constraint fails (phples.bugs, CONSTRAINT bugs_ibfk_1 FOREIGN KEY (oauth_uid2) REFERENCES users_facebook (oauth_uid2) ON DELETE CASCADE ON UPDATE CASCADE)

bugs table: FK = oauth_uid2, PK= bug_id

 #Name  Type    Collation   Attributes  Null    Default Extra   Action
     1  bug_id  int(30)         No  None    AUTO_INCREMENT    Change      Drop    Browse distinct values     Primary      Unique      Index  Spatial     Fulltext
     2  bugtitle    varchar(50) utf8_unicode_ci     No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial     Fulltext
     3  bugdescription  varchar(500)    utf8_unicode_ci     No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial     Fulltext
     4  oauth_uid2  int(30)         No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial     Fulltext

users_facebook table: PK= oauth_uid2

#   Name    Type    Collation   Attributes  Null    Default Extra   Action
     1  oauth_uid2  int(30)         No  None          Change      Drop    Browse distinct values     Primary      Unique      Index  Spatial     Fulltext
     2  email   varchar(70) utf8_unicode_ci     No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial     Fulltext
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You've already got the tables linked, which is where the error is coming from. You need to make sure that you have a record in users_facebook BEFORE you try to insert a record into bugs with the same oauth_uid2, e.g.

users_facebook has records with oauth_uid2 `10`, `20`, `30`

you try to insert a record into bugs with

INSERT INTO bugs (oauth_uid2) VALUES (10) // works, there's a matching record in users_facebook
INSERT INTO bugs (oauth_uid2) VALUES (15) // fails, there's no user with that id.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...