0

I have two tables: users and pics. They both have a pic_id attribute except the pics table references the pic_id in the users table.

Inside my users table: user_id: 15 password: test age: 18 pic_id: 0

I am trying to insert a row into my pics table with the pic_id attribute equal to the pic_id value for user 15.

I am trying to do all of this in PHPMyAdmin and I am getting the Cannot add or update a child row: a foreign key constraint fails error.

I read that the reason this error happens is because the value the referencing table is looking for does not exist; however in my case I am sure it does.

Attached is my users and pics table relationship: table relationship1

EDIT: Constraint Properties:

  CONSTRAINT `pics_ibfk_1`
    FOREIGN KEY (`pic_id`)
    REFERENCES `db`.`users` (`pic_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
Sean
  • 12,443
  • 3
  • 29
  • 47
Jack Lee
  • 35
  • 6
  • please show us the properties of the constraint. thanks. – t1t1an0 Nov 26 '15 at 04:27
  • @t1t1an0 added it in – Jack Lee Nov 26 '15 at 04:32
  • 1. check if both tables have the same data type for column `pic_id' 2. maybe the row you are pointing to in the `users` table is still uncommitted? 3. you might want to read this, http://stackoverflow.com/questions/5566991/mysql-5-5-foreign-key-constraint-fails-when-foreign-key-exists – t1t1an0 Nov 26 '15 at 05:09
  • Which table did you define the FK constraint *on*? It should be `CONSTRAINT pics_ibfk_1 FOREIGN KEY (pic_id) REFERENCES db.pics (pic_id)`. (I think.) – Darwin von Corax Nov 26 '15 at 05:13

0 Answers0