A foreign key constraint is used to stop you from inserting data into the table without a corresponding record existing in another table. The ID field in the table you are inserting into must match the ID field of a record in the table t1.
darkagn
Nearly a Posting Virtuoso
1,223 posts since Aug 2007
Reputation Points: 404
Solved Threads: 211
Skill Endorsements: 15
So you are trying to insert to t1 and you get that error? I must admit I didn't think it was possible to have such a foreign key constraint as one that references itself. I think if that's the case then it would probably be best to drop the FK constraint from the table, just make sure you don't drop the primary key constraint if it is also the PK of the table.
EDIT: And remember to backup the database beforehand so you can go back if this is the wrong move!
darkagn
Nearly a Posting Virtuoso
1,223 posts since Aug 2007
Reputation Points: 404
Solved Threads: 211
Skill Endorsements: 15
Are you able to check the FK on that table? You can do this at the mysql prompt like so:
mysql > SHOW CREATE TABLE t1\G
darkagn
Nearly a Posting Virtuoso
1,223 posts since Aug 2007
Reputation Points: 404
Solved Threads: 211
Skill Endorsements: 15
> Sorry about this, I really am at a loss with all this.
No need to apologise, that's why this forum is here :)
The foreign key in table t1 is definately referencing itself. And the field in question is also the PK of the table. This is very very very bad. What this says is "before you insert a new record, make sure that this record already exists!" Of course this is impossible.
My normal course of action here would be to drop the foreign key constraint from the table. Are you able to speak with whoever built the database to find out their intention with this foreign key? They might be able to tell you why they created it as they might have meant something else. Maybe they actually wanted to reference table t2's ID or something...
As I said earlier, backup the database, drop the constraint and see if this fixes your problem. Good luck :)
EDIT: And btw, table t2's FK constraint is ok.
darkagn
Nearly a Posting Virtuoso
1,223 posts since Aug 2007
Reputation Points: 404
Solved Threads: 211
Skill Endorsements: 15
Question Answered as of 4 Years Ago by
darkagn