gpt4 book ai didi

php - MySQL/Laravel 外键约束格式不正确

转载 作者:行者123 更新时间:2023-11-29 06:46:04 28 4
gpt4 key购买 nike

我正在尝试运行 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();
});

password_resets 表:

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 "外键约束的形成不正确")")

我猜订单表有问题,因为发生错误后我看不到数据库中的该表。其他表已创建。

最佳答案

由于您引用的是id,因此您需要将外键列设为无符号。因为 id 默认情况下是无符号的(非负)。

因此,对所有外键执行以下操作:

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

关于php - MySQL/Laravel 外键约束格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371608/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com