Have you dropped your original foreign key first?
Change foreign key to cascade instead of set null
hi artisans
Earlier, I made a foreign key that gets set to null after delete, like this:
$table->foreign('club_id')->references('id')->on('clubs')->onDelete('set null');
I would like to change that to cascade now instead. So I added the doctrine/dbal library as a dependency and did a new migration like this:
Schema::table('users', function($table){
$table->foreign('club_id')->references('id')->on('clubs')->onDelete('cascade')->change();
});
But when I run the migration, I get
SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
What am I doing wrong?
@Augustus I don't believe Laravel does anything to above and beyond what you;d have to do at the database directly; you need to remove the old FK constraint before creating a new one. Your constraint is separate to the actual field, so you can remove the constraint without deleting the actual column
$table->dropForeign('users_club_id_foreign');
Note backup your table before making changes!!
Please or to participate in this conversation.