jjosephs
139
13
Laravel

Can't create foreign key constraint - SQLSTATE[HY000]

Posted 3 years ago by jjosephs

The error I keep getting is

[Illuminate\Database\QueryException]                                                                                                                      
  SQLSTATE[HY000]: General error: 1005 Can't create table 'connector.#sql-5cc_405' (errno: 150) (SQL: alter table `patients` add constraint patients_cl  
  ient_id_foreign foreign key (`client_id`) references `clients` (`client_id`) on delete cascade) 

Tables being used

public function up()
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->integer('client_id')->unsigned();
            $table->string('client_first_name')->nullable();
            $table->string('client_last_name')->nullable();
            $table->string('client_email');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('patients', function (Blueprint $table) {
            $table->integer('patient_id');
            $table->string('patient_name')->nullable();
            $table->integer('client_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('patients', function(Blueprint $table){
            $table->foreign('client_id')->references('client_id')->on('clients')->onDelete('cascade');
        });
    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.