NM.Riyas
NM.Riyas

Reputation: 19

How to debug errno: 150 "Foreign key constraint is incorrectly formed" in Laravel?

Here I have three tables - customers | orders | services.

I am trying to add customers and services table's ID as foreign key in orders table, but getting an error while migrating as error 150. I'm a new learner of Laravel framework. How can I debug this?

   public function up()
   {
       Schema::create('customers', function (Blueprint $table) {
           $table->increments('id');
           $table->string('first_name');
           $table->string('last_name');
           $table->string('nic', 12)->unique();
           $table->string('address');
           $table->integer('phone_number', 10)->unique();
           $table->integer('gender_id')->unsigned();
           $table->date('dob');

           $table->foreign('gender_id')->references('id')->on('genders');
           $table->timestamps();
           $table->softDeletes();

       });
   }


public function up()
   {
       Schema::create('services', function (Blueprint $table) {
           $table->increments('id');
           $table->string('service_name');
           $table->timestamps();
       });
   }

public function up()
   {
       Schema::create('orders', function (Blueprint $table) {
           $table->increments('id');
           $table->integer('service_id')->unsigned();
           $table->string('remark')->nullable();
           $table->integer('customer_id')->unsigned();
           $table->timestamps();

           $table->foreign('customer_id')->references('id')->on('customers');
           $table->foreign('service_id')->references('id')->on('services');

       });
   }

Error message:

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `ocsas`.`orders` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `orders` add constraint `orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`id`))

  at C:\xampp\htdocs\ocsas\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:
  669:         return $result;

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `ocsas`.`orders` (errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\ocsas\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 458

  2   PDOStatement::execute()
      C:\xampp\htdocs\ocsas\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 458

  Please use the argument -v to see more details.

Upvotes: 0

Views: 212

Answers (1)

Loot
Loot

Reputation: 1

You should mark customers.id and services.id as UNSIGNED

Schema::create('customers', function (Blueprint $table) {
    $table->increments('id')->unsigned();
});
Schema::create('services', function (Blueprint $table) {
    $table->increments('id')->unsigned();
});

Upvotes: 0

Related Questions