I want to create relationship between user and budget. There are 2 foreignkey in budget table.
- requestor_id
- approver_id
All is linked to user table.
If record in table users is deleted. I want to delete budget table also, if requestor or approver using that deleted record.
here is my migration
users table :
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Budgets table:
public function up()
{
Schema::create('budgets', function (Blueprint $table) {
$table->id();
$table->foreignId('requestor_id')->nullable()
->references('id')->on('users')
->cascadeOnDelete();
$table->foreignId('approver_id')->nullable()
->references('id')->on('users')
->cascadeOnDelete();
$table->timestamps();
});
}
But when I tried to migration, I got error :
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Introducing FOREIGN KEY constraint 'budgets_approver_id_foreign' on table 'budgets' 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 "budgets" add constraint "budgets_approver_id_foreign" foreign key ("approver_id") references "users" ("id") on delete cascade)
How I can fix this error ?
Note: I am using SQL-Server 2018
Thank you