Foreign key constraint fails

Thread Solved

Join Date: Nov 2007
Posts: 8
Reputation: br1dil is an unknown quantity at this point 
Solved Threads: 0
br1dil br1dil is offline Offline
Newbie Poster

Foreign key constraint fails

 
0
  #1
Jan 8th, 2009
Hello everyone,

At first, I would like to wish an happy new year for everyone.

Now, my problem! I'm starting a new web app (which is the first one I will completely write) and after modeling the database, I encoutered a problem for adding datas in my category's table:
  1. --
  2. -- Structure de la table `categories`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `categories` (
  6. `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  7. `name` VARCHAR(100) DEFAULT NULL,
  8. `parent_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  9. `alias` VARCHAR(20) DEFAULT NULL,
  10. `image` VARCHAR(255) DEFAULT NULL,
  11. `ordering` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  12. `system` VARCHAR(20) DEFAULT NULL,
  13. `lft` INT(10) NOT NULL,
  14. `rght` INT(10) NOT NULL,
  15. `created` DATETIME NOT NULL,
  16. `modified` DATETIME NOT NULL,
  17. `visible` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
  18. PRIMARY KEY (`id`),
  19. KEY `fk_categories_categories` (`parent_id`)
  20. ) ENGINE=INNODB DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=1 ;
  21.  
  22. --
  23. -- Contenu de la table `categories`
  24. --
  25.  
  26.  
  27. --
  28. -- Contraintes pour les tables exportées
  29. --
  30.  
  31. --
  32. -- Contraintes pour la table `categories`
  33. --
  34. ALTER TABLE `categories`
  35. ADD CONSTRAINT `fk_categories_categories` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Even if I try to add some entries directly with phpmyadmin, I get the following message:

  1. SQL Error: 1452: Cannot add OR UPDATE a child row: a FOREIGN key CONSTRAINT fails (`myapp`.`categories`, CONSTRAINT `fk_categories_categories` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

I'm not quite familiar with the INNODB engine and I assumed this error is linked with the foreign key parent_id. I don't understand why MySQL is sending an error.

I'll be glad if someone has an answer for explaining this.

Thank you guys.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: Foreign key constraint fails

 
0
  #2
Jan 9th, 2009
Originally Posted by br1dil View Post
Hello everyone,

At first, I would like to wish an happy new year for everyone.

Now, my problem! I'm starting a new web app (which is the first one I will completely write) and after modeling the database, I encoutered a problem for adding datas in my category's table:
  1. --
  2. -- Structure de la table `categories`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `categories` (
  6. `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  7. `name` VARCHAR(100) DEFAULT NULL,
  8. `parent_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  9. `alias` VARCHAR(20) DEFAULT NULL,
  10. `image` VARCHAR(255) DEFAULT NULL,
  11. `ordering` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  12. `system` VARCHAR(20) DEFAULT NULL,
  13. `lft` INT(10) NOT NULL,
  14. `rght` INT(10) NOT NULL,
  15. `created` DATETIME NOT NULL,
  16. `modified` DATETIME NOT NULL,
  17. `visible` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
  18. PRIMARY KEY (`id`),
  19. KEY `fk_categories_categories` (`parent_id`)
  20. ) ENGINE=INNODB DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=1 ;
  21.  
  22. --
  23. -- Contenu de la table `categories`
  24. --
  25.  
  26.  
  27. --
  28. -- Contraintes pour les tables exportées
  29. --
  30.  
  31. --
  32. -- Contraintes pour la table `categories`
  33. --
  34. ALTER TABLE `categories`
  35. ADD CONSTRAINT `fk_categories_categories` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Even if I try to add some entries directly with phpmyadmin, I get the following message:

  1. SQL Error: 1452: Cannot add OR UPDATE a child row: a FOREIGN key CONSTRAINT fails (`myapp`.`categories`, CONSTRAINT `fk_categories_categories` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

I'm not quite familiar with the INNODB engine and I assumed this error is linked with the foreign key parent_id. I don't understand why MySQL is sending an error.

I'll be glad if someone has an answer for explaining this.

Thank you guys.
hi Br1Dil,

Well from first looks, the last column you added has a foreign key constraint referencing the same table. Is that constraint necessary ? I don't think so. Try commenting it out, or removing it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: Foreign key constraint fails

 
0
  #3
Jan 9th, 2009
Originally Posted by wilch View Post
hi Br1Dil,

Well from first looks, the last column you added has a foreign key constraint referencing the same table. Is that constraint necessary ? I don't think so. Try commenting it out, or removing it.
else you can specify a second different table in the fk constraint e.g. rather than fk_table1_table1(column) make it fk_table1_table2(column)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 8
Reputation: br1dil is an unknown quantity at this point 
Solved Threads: 0
br1dil br1dil is offline Offline
Newbie Poster

Re: Foreign key constraint fails

 
0
  #4
Jan 9th, 2009
Hi wilch, thank you for replying.

I didn't use the foreign key volountary. It was the way MySQL Workbench create the table Categories. The field 'parent_id' is referring to the same table 'categories' to have an hierarchical treelist of categories.
I tried to delete the index but it doesn't work in phpmyadmin, I got this error:
  1. #1025 - Error on rename of '.\myapp\#sql-9cc_12' to '.\myapp\categories' (errno: 150)

So I tried to delete the table but I got this error too:
  1. #1217 - Cannot delete or update a parent row: a foreign key constraint fails

Why do I have this error? How can I get around it?
This table will contain all the categories used in my app, I will use the adjacency list model and the nested set model too. Is it better to use INNODB than Myisam? What do you think?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 8
Reputation: br1dil is an unknown quantity at this point 
Solved Threads: 0
br1dil br1dil is offline Offline
Newbie Poster

Re: Foreign key constraint fails

 
0
  #5
Jan 9th, 2009
Ok, I finally deleted the constraint.
First, I had to delete the foreign key:
  1. ALTER TABLE `categories` DROP FOREIGN KEY `fk_categories_categories`;
and after the index key:
  1. ALTER TABLE `categories` DROP INDEX `fk_categories_categories`
So now, I don't have any constraint in my table and I can add datas but I'm wondering if I should use foreign key constraint, is it worth it?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Foreign key constraint fails

 
0
  #6
Jan 9th, 2009
While specifying the foreign key constraint you have specified "ON UPDATE NO ACTION" while this seems to mean that no action would be taken on update, it specifically means that you will not be allowed to update too, either remove the clause or if it is binding for you to have it, modify it suitably.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MySQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC