Я работаю над проектом, в котором я использую laravel 5.8.Здесь я хочу создать внешний ключ.Я пробовал много времени, но каждый раз получал сообщение об ошибке во время миграции.Есть три таблицы, одна из которых - это торговля, которая является первичной, и студенты, места размещения - это вторичная таблица.Я хочу создать внешний ключ для этих двух вторичных таблиц trade_id в качестве имени ключа.
Это таблица сделок (первичная таблица).
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTradesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('trades', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('trade', 32)->nullable();
$table->string('unit', 32)->nullable();
$table->string('class_room', 32)->nullable();
$table->string('total_class_room', 32)->nullable();
$table->string('workshop', 32)->nullable();
$table->string('total_workshops', 32)->nullable();
$table->string('remarks', 32)->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('trades');
}
}
Это таблица учеников (вторичная таблица).
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStudentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 32)->nullable();
$table->string('father_name', 32)->nullable();
$table->string('uid', 12)->nullable();
$table->date('dob');
$table->string('sex', 6)->nullable();
$table->string('status', 16)->nullable();
$table->integer('trade_id')->unsigned()->nullable();
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
$table->string('shift', 32)->nullable();
$table->string('session', 32)->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('students');
}
}
Это таблица мест размещения (вторичная таблица).
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePlacementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('placements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('roll_no', 32)->nullable();
$table->string('name', 32)->nullable();
$table->integer('trade_id')->unsigned()->nullable();
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
$table->year('year_of_passing');
$table->string('organization_name', 32)->nullable();
$table->float('salary_on_joining', 8,2);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('placements');
}
}
Во время миграции возникает ошибка.
$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_05_22_132427_create_notices_table
Migrated: 2019_05_22_132427_create_notices_table
Migrating: 2019_05_22_132600_create_students_table
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `students` add co
nstraint `students_trade_id_foreign` foreign key (`trade_id`) references `trades` (`id`) on delete cascade)
at C:\laragon\www\projects\UIIT-admin\uiti_admin\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
C:\laragon\www\projects\UIIT-admin\uiti_admin\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\laragon\www\projects\UIIT-admin\uiti_admin\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
Please use the argument -v to see more details.