Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL - Foreign Key, constraints error..

Options
  • 17-04-2009 11:53pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Ok,

    I'm stumped! I'm trying to setup a foreign key realtionship as shown below..
    I keep getting the same error which is annoying. I've done this kind of thing with other fields but this just doesn't seem to be working at all...

    I'm sure it's something small but what am I doing wrong here??:o
    SQL query:
    
    ALTER TABLE `users` ADD FOREIGN KEY ( `profile_photo` ) REFERENCES `iflicks`.`photos` (
    `photo_id`
    ) ON DELETE CASCADE ON UPDATE CASCADE ;
    
    MySQL said: Documentation
    #1452 - Cannot add or update a child row: a foreign key constraint fails (`iflicks`.`#sql-4dc_221`, CONSTRAINT `#sql-4dc_221_ibfk_1` FOREIGN KEY (`profile_photo`) REFERENCES `photos` (`photo_id`) ON DELETE CASCADE ON UPDATE CASCADE) 
    

    Thanks..


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    If the FK constraint was in place, one of the rows would violate it so when you try to alter the table. Although that's a massive assumption on my part because I don't know if MySQL is that "strict". Or it could be a circular dependency?


  • Registered Users Posts: 2,234 ✭✭✭techguy


    fasty wrote: »
    If the FK constraint was in place, one of the rows would violate it so when you try to alter the table. Although that's a massive assumption on my part because I don't know if MySQL is that "strict". Or it could be a circular dependency?

    You are correct, I found something just after I posted..

    I just cleared the data in both tables and it worked fine..now I have problems adding rows but thats probably something just as trivial..

    Thanks.


Advertisement