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

yahia1001's avatar

database error

SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'applicants' (SQL: alter table students add constraint students_app_id_foreign foreign key (app_id) references applicants (Applicant_id) on delete cascade) i get this error when i run migrate or migrate:fresh in cmd and he are the two tables public function up() { Schema::create('students', function (Blueprint $table) { $table->increments('Student_id'); $table->unsignedInteger('app_id'); $table->string('name'); $table->string('phone'); $table->char('email'); $table->string('faculty'); $table->timestamps(); $table->foreign('app_id') ->references('Applicant_id') ->on('applicants') ->onDelete('cascade'); }); }

public function up() { Schema::create('applicants', function (Blueprint $table) { $table->increments('Applicant_id'); $table->string('name'); $table->string('Highschooltype'); $table->float('Grade'); $table->integer('National_ID'); $table->char('email'); $table->string('Highschool country'); $table->string('Birth country'); $table->string('faculty'); $table->date('Date of birth'); $table->binary('Image'); $table->binary('Certificate'); $table->binary('Birth Certificate'); $table->timestamps(); }); } what could be causing the error?

0 likes
15 replies
sr57's avatar

@yahia1001

migration of table students depends on table applicants ( foreign ) that does not still exist.

the easiest way is to create a global migration for both tables

up : create applicants and then students

down : drop students and then applicants

yahia1001's avatar

@sr57 i am sorry i do not get what u are saying and both tables exist and up : create applicants and then students

down : drop students and then applicants how to do this

yahia1001's avatar

@sr57 donot u mean the classes that i have written on here?

public function up() { Schema::create('students', function (Blueprint $table) { $table->increments('Student_id'); $table->unsignedInteger('app_id'); $table->string('name'); $table->string('phone'); $table->char('email'); $table->string('faculty'); $table->timestamps(); $table->foreign('app_id') ->references('Applicant_id') ->on('applicants') ->onDelete('cascade'); }); }

public function up() { Schema::create('applicants', function (Blueprint $table) { $table->increments('Applicant_id'); $table->string('name'); $table->string('Highschooltype'); $table->float('Grade'); $table->integer('National_ID'); $table->char('email'); $table->string('Highschool country'); $table->string('Birth country'); $table->string('faculty'); $table->date('Date of birth'); $table->binary('Image'); $table->binary('Certificate'); $table->binary('Birth Certificate'); $table->timestamps(); }); }

siangboon's avatar

the sequence of the table creation does matter, applications table have to created before students as students rely on application...

yahia1001's avatar

@siangboon and how do i create applicants first do i got recreate the tables and make apps first?

sr57's avatar

@sr57

Try writing your migration like this

up : create applicants and then students

public function up() 
{ 
     Schema::create('applicants', ....
     Schema::create('students', ....
}

down : drop students and then applicants

 public function down()
 {
        Schema::dropIfExists('students');
        Schema::dropIfExists('applicants');
 }

PS : rollback and delete your previous migration files before for these 2 tables;

yahia1001's avatar

ok so i created the tables all over again but now i am getting this error SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'hostels' already exists (SQL: create table hostels (Hostel_id int unsigned not null auto_increment primary key, ha_id int unsigned not null, Duration varchar(191) not null, Number of rooms int not null, Available rooms int not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

sr57's avatar

Table 'hostels'

Nothing to do with the current question, you probably have a "mess" with your migrations. Delete (put in a backup directory) the migration file for hotels.

sr57's avatar

@yahia1001

You misunderstand, a rollback does not delete all tables but only the last migration(s).

Never mind, if you did a "fresh" and have delete all you tables, the pb with "hôtels"means you have probably 2 migrations for these table. Review all your migration files, delete the duplicate(s) and then redo migrate.

yahia1001's avatar

@sr57 so i deleted the duplicates should i now run migrate:fresh then migrate again?

yahia1001's avatar

even when i run migrate:fresh i get this SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') on delete cascade' at line 1 (SQL: alter table hostels add constraint hostels_ha_id_foreign foreign key (ha_id) references hostel_applicants () on delete cascade)

sr57's avatar

@yahia1001

Please make it simple, don't mix your question, remove your migration from hostels and go ahead to close the initial question. Then if you, still have a pb with 'hostels' , you'll open a new question.

Please or to participate in this conversation.