【问题标题】:MySQL/Laravel Foreign key constraint is incorrectly formedMySQL/Laravel 外键约束的格式不正确
【发布时间】:2018-03-19 20:12:32
【问题描述】:

我正在尝试运行 php artisan migrate 以使用 laravel 创建我的 mysql 表。

我收到了这个错误: 外键约束格式不正确

用户表:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('street');
            $table->string('city');
            $table->string('phone');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

密码重置表:

Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

产品表:

 Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('product_type');
            $table->integer('quantity');
            $table->timestamps();
        });

出货表:

  Schema::create('shipments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('order_number')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->foreign('order_number')->references('id')->on('orders');
        $table->foreign('product_id')->references('id')->on('products');
        $table->dateTime('chargecardtime');
        $table->dateTime('packingtime');
        $table->date('shiporderdate');
        $table->timestamps();
    });

订单表:

    Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('customer_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->foreign('customer_id')->references('id')->on('users');
        $table->foreign('product_id')->references('id')->on('products');
        $table->string('name');
        $table->string('to_street');
        $table->string('to_city');
        $table->date('ship_date');  
        $table->string('phone');
        $table->timestamps();
    });

异常跟踪:

1 PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表ec.#sql-3664_86 (errno: 150 "外键约束格式不正确")")

我猜orders表有问题,因为出错后我在database.others中看不到该表。

【问题讨论】:

  • 哪些语句会失败?
  • 我看不到
  • 我猜orders表有问题,因为出错后我在database.others中看不到该表。

标签: php mysql laravel


【解决方案1】:

由于您引用的是id,因此您需要创建外键列unsigned。因为 id 默认是无符号的(非负数)。

对所有外键执行此操作:

$table->integer('product_id')->unsigned();

【讨论】:

  • 确保您确实删除了所有以前创建的字段并在新数据库上运行迁移。
  • 在数据库中检查最后创建的表以及迁移中的下一个表。这样你就可以很容易地分辨出哪个失败了。确保它们符合您发布它们的顺序
【解决方案2】:

您应该将unsigned() 方法添加到您的外键列,例如$table->integer('product_id')->unsigned(),因为它们必须与所引用的键的列类型完全相同。引用的键可能是无符号整数,这就是您收到错误的原因。在 MySQL 中,有符号整数和无符号整数是不同的类型

【讨论】:

    【解决方案3】:

    $table->integer('product_id')->unsigned();

    试试这个

    $table->unsignedBigInteger(');

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 2019-07-25
      • 2018-09-04
      • 1970-01-01
      • 2017-10-04
      • 2018-02-19
      • 2021-05-26
      • 2020-09-24
      • 2019-11-02
      相关资源
      最近更新 更多