| | |
Foreign key constraint fails
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 8
Reputation:
Solved Threads: 0
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:
Even if I try to add some entries directly with phpmyadmin, I get the following message:
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.
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:
MySQL Syntax (Toggle Plain Text)
-- -- Structure de la table `categories` -- CREATE TABLE IF NOT EXISTS `categories` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) DEFAULT NULL, `parent_id` INT(10) UNSIGNED NOT NULL DEFAULT '0', `alias` VARCHAR(20) DEFAULT NULL, `image` VARCHAR(255) DEFAULT NULL, `ordering` INT(10) UNSIGNED NOT NULL DEFAULT '0', `system` VARCHAR(20) DEFAULT NULL, `lft` INT(10) NOT NULL, `rght` INT(10) NOT NULL, `created` DATETIME NOT NULL, `modified` DATETIME NOT NULL, `visible` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `fk_categories_categories` (`parent_id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=1 ; -- -- Contenu de la table `categories` -- -- -- Contraintes pour les tables exportées -- -- -- Contraintes pour la table `categories` -- ALTER TABLE `categories` 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:
MySQL Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
•
•
•
•
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:
MySQL Syntax (Toggle Plain Text)
-- -- Structure de la table `categories` -- CREATE TABLE IF NOT EXISTS `categories` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) DEFAULT NULL, `parent_id` INT(10) UNSIGNED NOT NULL DEFAULT '0', `alias` VARCHAR(20) DEFAULT NULL, `image` VARCHAR(255) DEFAULT NULL, `ordering` INT(10) UNSIGNED NOT NULL DEFAULT '0', `system` VARCHAR(20) DEFAULT NULL, `lft` INT(10) NOT NULL, `rght` INT(10) NOT NULL, `created` DATETIME NOT NULL, `modified` DATETIME NOT NULL, `visible` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `fk_categories_categories` (`parent_id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=1 ; -- -- Contenu de la table `categories` -- -- -- Contraintes pour les tables exportées -- -- -- Contraintes pour la table `categories` -- ALTER TABLE `categories` 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:
MySQL Syntax (Toggle Plain Text)
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.
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.
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
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)
•
•
Join Date: Nov 2007
Posts: 8
Reputation:
Solved Threads: 0
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:
So I tried to delete the table but I got this error too:
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
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:
MySQL Syntax (Toggle Plain Text)
#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:
MySQL Syntax (Toggle Plain Text)
#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
•
•
Join Date: Nov 2007
Posts: 8
Reputation:
Solved Threads: 0
Ok, I finally deleted the constraint.
First, I had to delete the foreign key:
and after the index key:
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?
First, I had to delete the foreign key:
MySQL Syntax (Toggle Plain Text)
ALTER TABLE `categories` DROP FOREIGN KEY `fk_categories_categories`;
MySQL Syntax (Toggle Plain Text)
ALTER TABLE `categories` DROP INDEX `fk_categories_categories`
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 !!!
![]() |
Similar Threads
- Foreign Keys (MySQL)
Other Threads in the MySQL Forum
- Previous Thread: MySql Licensing query
- Next Thread: excel to mysql
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright court crm database design developer development distinct dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





