Skip to content
Advertisement

Laravel foreign key onDelete(‘cascade’) not working

I have a many-to-many relationship between User & Role, with a role_user table. My migrations are setup as so (simplified):

users table:

roles table:

role_user table:

So as per the docs, I set my foreign keys to unsigned.

Now, I add a couple of users, and attach some roles – everything works fine. However, when I delete a user (User::destroy(2)) the rows for that user in the role_user table do not get deleted, which is causing redundant rows.

What am I doing wrong?

  • MySQL + InnoDB

EDIT: Grabbing the model and applying ->delete(); also has the same effect.

Advertisement

Answer

Try setting when trying to create this table. This fix has worked for me.

I have filed a bug under: https://github.com/laravel/framework/issues/8730

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement