excuse me, I want to ask about error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table activity_student add constraint activity_student_student_id_foreign foreign key (student_id) references students (id) on delete cascade on update cascade)
when I run the php artisan migrate:fresh command an error appears like that
please help with the solution
The following is the coding of some of my tables:
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('phone');
$table->string('email');
$table->string('address');
$table->foreignId('student_id')->constrained('students')->cascadeOnUpdate()->cascadeOnDelete();
$table->timestamps();
});
}
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('score');
$table->foreignId('teacher_id')->constrained('teachers')->cascadeOnUpdate()->cascadeOnDelete();
$table->timestamps();
});
}
public function up()
{
Schema::create('activity_student', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreignId('activity_id')->constrained('activities')->cascadeOnUpdate()->cascadeOnDelete();
$table->timestamps();
});
}
It's either the order of the migrations that is wrong, the table referenced must exist before you can create a foreign key, or the data type doesn't match between the columns.