Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql error #1005 - Can't create table

I write a query when doing a tutorial Agile web application development with yii. I was trying to alter a table with this query and got this error in phpmyadin

#1005 - Can't create table 'trackstar_dev.#sql-152_16' (errno: 121) (<a href="server_engines.php?engine=InnoDB&amp;page=Status&amp;token=af80b3320dff7dfda18d88d97fe80da4">Details...</a>)

Here is my query

ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_
user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON
DELETE CASCADE ON UPDATE RESTRICT

Can Anyone help please ?

like image 699
Ashfaq Rahman Avatar asked Dec 08 '25 13:12

Ashfaq Rahman


1 Answers

This happens when table tbl_project_user_assignment has already records which project_id is not found on the table (which you want to reference) tbl_project.id. The best way you can do is to empty the tbl_project_user_assignment and alter it again to add the constraint.

like image 182
John Woo Avatar answered Dec 10 '25 02:12

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!