0

So I have a pretty basic CRUD application with 2 models. Website and Category. Website consists of a name, a link and a category_id (which is a reference to the name of Category). Category consists of a name (which is the primary key) and an icon. When I want to update the Category model, I send my API a request with the new name, the original name and the icon. When I try to save the model, I get an error:

"SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`startpage`.`website`, CONSTRAINT `website_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`name`)) (SQL: update `categories` set `name` = Search Engines where `name` = Search Egnines)"

It is obvious that the problem lies in trying to update an attribute that is used to define relations between the tables. My question therefore is, is there a way for me to update the attribute and simultaneously update all references to it, so the relations stay intact?

Thanks in advance!

  • 2
    Update cascade? https://stackoverflow.com/questions/1481476/when-to-use-on-update-cascade – Felippe Duarte Aug 09 '19 at 14:28
  • 2
    Update cascade should work but ideally there should be an id column on the categories table which would act as the primary key instead. The current architecture could become problematic as the app scales. – user1669496 Aug 09 '19 at 14:30
  • @FelippeDuarte Thank you! That was indeed what I was looking for. user1669496 yeah normally I would've rewritten the model like that, but now it only concerns an internal pilot application for the team to get used to Laravel, so this way is sufficient for now. – Lukas Derksen Aug 11 '19 at 12:28

0 Answers0