Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL - Foreign Key, constraints error..

  • 17-04-2009 10:53PM
    #1
    Registered Users, Registered Users 2 Posts: 2,238 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 2,238 ✭✭✭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