Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ssquare's avatar

Introducing FOREIGN KEY constraint 'my_list_user_user_id_foreign' on table 'my_l ist_user' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I have just moved from mysql to MSSQL server. But, if I try to run migration I received error as follows. [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Introducing FOREIGN KEY constraint 'my_list_user_user_id_foreign' on table 'my_l ist_user' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. (SQL: alter table "my_list_user" add constrai nt "my_list_user_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade)

My migration schema looks lie belows

Users

        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->integer('organization_id')->unsigned();
            $table->foreign('organization_id')->references('id')->on('organizations');
            $table->rememberToken();
            $table->softDeletes();
            $table->timestamps();
        });

My_Lists

        Schema::create('my_lists', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title',255); 
            $table->text('description'); 

            $table->unsignedInteger('created_by');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');

            $table->softDeletes();
            $table->timestamps();
        });

My_list_user

        Schema::create('my_list_user', function (Blueprint $table) {
            $table->integer('my_list_id')->unsigned()->index();
            $table->foreign('my_list_id')->references('id')->on('my_lists')->onDelete('cascade');


            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->primary(['my_list_id', 'user_id']); 
         });

Here, created by means the one who created the list and user_id from pivot tables refers to the users who have access to the list.

Is this some thing I am making over comlpexed? How can I overcome this problem with MS SQL?

Is that MS SQL does not support even a single level of cascade? How can I solve this?

0 likes
6 replies
Tray2's avatar

I think this is the line it complains about.

 $table->foreign('organization_id')->references('id')->on('organizations');

Since you have no actions defined on what should happen when the user is deleted.

so by adding ->onDelete('no action')to it might solve the issue.

Inquisitive's avatar

It might be saying this because it is connected to the user's table by two means creating a cycle. What about removing created by from my_lists and instead adding a is_owner column to the pivot table?

ssquare's avatar

@Inquisitive It runs migration without error but If follows that, I need to make a lot of changes. Is there any other idea?

Inquisitive's avatar
Level 9

@ssquare I have seen that you are using cascade on delete even for the user who access it. I don't think you need to delete the list even if the user who is not the creator but only have access to it. So update following

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

with no action

$table->foreign('user_id')->references('id')->on('users')->onDelete('no action');

Please or to participate in this conversation.