Php MySQL/Laravel外键约束格式不正确
我正在尝试运行php artisan migrate,以使用laravel创建我的mysql表 我得到了这个错误: 外键约束的格式不正确 用户表:Php MySQL/Laravel外键约束格式不正确,php,mysql,laravel,Php,Mysql,Laravel,我正在尝试运行php artisan migrate,以使用laravel创建我的mysql表 我得到了这个错误: 外键约束的格式不正确 用户表: Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('street');
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\u 86(错误号:150“外键约束格式不正确”))
我猜orders表有问题,因为出错后我在数据库中看不到该表。其他表已创建。因为您引用的是
id
,所以需要使外键列无符号。As ID默认为无符号(非负)
因此,请对所有外键执行以下操作:
$table->integer('product_id')->unsigned();
由于您引用的是id
,因此需要使外键列不带符号
。As ID默认为无符号(非负)
因此,请对所有外键执行以下操作:
$table->integer('product_id')->unsigned();
您应该向外键列添加unsigned()
方法,如$table->integer('product_id')->unsigned()
,因为它们必须与引用的键的列类型完全相同。引用的键可能是无符号整数,这就是为什么会出现错误。在MySQL中,整数有符号和整数无符号是不同的类型您应该向外键列添加unsigned()
方法,如$table->integer('product_id')->unsigned()
,因为它们必须与引用的键完全相同的列类型。引用的键可能是无符号整数,这就是为什么会出现错误。在MySQL中,整数有符号和整数无符号是不同的类型
试试这个
$table->unsignedBigInteger(') $table->integer('product_id')->unsigned()
试试这个
$table->unsignedBigInteger(') 哪些语句失败?我看不到,我想orders表有问题,因为出错后我看不到数据库中的表。其他语句已创建。哪些语句失败?我看不到,我想orders表有问题,因为出错后我看不到数据库中的表。其他语句已创建。请确保您确实删除了以前创建的所有字段,并在新数据库上运行了迁移。请在数据库中检查上次创建的表以及迁移中下一个应该创建的表。这样你就可以很容易地分辨出哪个失败了。确保它们是按顺序发布的。确保确实删除了所有以前创建的字段,并在新数据库上运行迁移。在数据库中检查上次创建的表以及迁移中下一个应创建的表。这样你就可以很容易地分辨出哪个失败了。确保它们是按顺序张贴的