0

这里我有三张桌子 -客户 | 订单 | 服务

我正在尝试将客户和服务表的 ID 作为外键添加到订单表中,但在迁移时出现错误 150。我是 Laravel 框架的新学习者。我该如何调试呢?

   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');

       });
   }

错误信息:

   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.

4

1 回答 1

0

您应该将customers.idand标记services.id为 UNSIGNED

Schema::create('customers', function (Blueprint $table) {
    $table->increments('id')->unsigned();
});
Schema::create('services', function (Blueprint $table) {
    $table->increments('id')->unsigned();
});
于 2020-02-06T04:39:02.567 回答