Kanchan186
1 month ago
153
8
Laravel

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `db_register`.`registers` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `registers` add constraint `registers_s_id_foreign` forei

Posted 1 month ago by Kanchan186

registers table

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRegistersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('registers', function (Blueprint $table) {
            $table->bigIncrements('u_id');
            $table->string('name');
            $table->string('country');
            $table->string('state');
            $table->string('city');
           // $table->string('hobby');

            $table->timestamps();
        });

    Schema::table('registers', function (Blueprint $table) 
    {
            $table->bigInteger('c_id')->unsigned();
            $table->foreign('c_id')->references('c_id')->on('countries')->onDelete('cascade');

             $table->bigInteger('s_id')->unsigned();
             $table->foreign('s_id')->references('s_id')->on('states')->onDelete('cascade');

             $table->bigInteger('ct_id')->unsigned();
             $table->foreign('ct_id')->references('ct_id')->on('cities')->onDelete('cascade');

     /*         $table->bigInteger('h_id')->unsigned();
             $table->foreign('h_id')->references('h_id')->on('hobbies')->onDelete('cascade');     */


            });




    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('registers');
         Schema::table('registers', function (Blueprint $table) {
            $table->dropForeign('registers_c_id_foreign');
            $table->dropForeign('registers_s_id_foreign');
            $table->dropForeign('registers_ct_id_foreign');
           // $table->dropForeign('registers_h_id_foreign');
             });
    
    }
}

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