A foreign key constraint fails

A foreign key constraint fails

I have set up a donations_type table:

And migrated that, all good.

Now I want to add a new column to my donations table that references these donation_types:

Upon migrate of that, I get this:

Can’t for the life of me figure out where the problem is!

Any help very much appreciated.

Please sign in or create an account to participate in this conversation.

I am relatively new in php and mysql.The problem that i am facing while i inserting value in my leave table.My leave table containing following column..

1.lid(INT primary key)


here empID is the foreign key references from users table. The problem that im facing while inserting values into the leave table.ERROR is given below

Cannot add or update a child row: a foreign key constraint fails ( db_attendance1 . leave , CONSTRAINT leave_ibfk_1 FOREIGN KEY ( empID ) REFERENCES users ( empID ))

here i just send the query and here it is..

3 Answers 3

and run your query. Once you are done again set it back to 1 by

A foreign key constraint means that you one table doesn’t accept inserts, updates or deletes that would ‘break’ the foreign key. This means, you can’t update a EmpID if the new EmpID doesn’t exist in the users. You can’t add a new EmpID if it doesn’t exist in the users table, etcetera. So to solve this issue, you need to make sure that the EmpID you’re trying to add to table ‘leave’, first exists in table ‘users’. Foreign keys can be a real powerful item, but can be a real pain too. Since the DB you’re working on had foreign key constraints, I suggest you read on them a bit: http://en.wikipedia.org/wiki/Foreign_key

Читайте также:  Ps4 игры в разработке

Borniet, you helped me solve my similar problem.

@OP — All I had to do to fix this was create a corresponding row in the table so that the foreign key would exist. E.g. Table 1 has column Name Table 2 has column friends_name, a foreign key tied to Name in table 1. I got this error because I was trying to insert a row into table 2, where the friends_name referenced a non existing Name in table 1. So I created the name and we’re off to the races :).

Not the answer you’re looking for? Browse other questions tagged php mysql or ask your own question.



Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.3.23.36344

currently i’m working on a MySQL Database.

For visualize the database, i set some foreign keys inside. In most cases it works perfectly, but in two cases not.

[23000][1452] (conn=604) Cannot add or update a child row: a foreign key constraint fails (`hilland`.`#sql-d14_25c`, CONSTRAINT `tbl_uebergabe_details_tbl_uebergabe_ID_fk` FOREIGN KEY (`UebergabeID`) REFERENCES `tbl_uebergabe` (`ID`))

Ссылка на основную публикацию
Adblock detector