Когда я php artisan migrate
, тогда я получаю сообщение об ошибке, см. Ниже.
Заказать миграцию пользователей, компаний и сводных миграций.
Когда я удаляю user
, все companies
должны быть удалены, а когда я удаляю, company
все users
должны быть удалены.
Что я делаю не так?
User.php
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Companies.php
Schema::create('companies', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id')->unsigned();
$table->string('companyname');
$table->string('address');
$table->integer('housenumber');
$table->string('postalcode');
$table->string('city');
$table->string('province');
$table->string('email');
$table->string('phonenumber');
$table->timestamps();
});
CreateUserCompanyPivotTable.php
Schema::create('user_company', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
});
Schema::table('user_company', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});
Ошибка:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215
Cannot add foreign key constraint (SQL: alter table `user_company` add constraint `user_company_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)