errno: 150 外键约束的格式不正确
errno: 150 Foreign key constraint is incorrectly formed
我有 laravel 5.5 个项目
在迁移中我写了这个
Schema::create('item_gifts', function (Blueprint $table) {
$table->increments('id');
$table->string('item_gift_name');
$table->integer('item_gift_item_id_from')->unsigned();
$table->foreign('item_gift_item_id_from')->references('id')->on('items');
$table->integer('item_gift_item_id_to')->unsigned();
$table->foreign('item_gift_item_id_to')->references('id')->on('items');
$table->integer('item_gift_quantity');
$table->integer('item_gift_min_order');
$table->timestamps();
});
但我总是遇到这个错误
In Connection.php line 664:
SQLSTATE[HY000]: General error: 1005 Can't create table `lar
avel`.`#sql-2830_22f` (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table `item_gifts` add con
straint `item_gifts_user_id_foreign` foreign key (`user_id`)
references `items` (`id`))
In Connection.php line 458:
SQLSTATE[HY000]: General error: 1005 Can't create table `lar
avel`.`#sql-2830_22f` (errno: 150 "Foreign key constraint is
incorrectly formed")
我需要的是将 item_gifts 中的 item_gift_item_id_from 与项目中的 id 连接起来
我尝试了很多解决方案,但没有任何效果
谢谢..
试试这个代码:
public function up()
{
Schema::create('item_gifts', function($table) {
$table->increments('id');
$table->string('item_gift_name');
$table->integer('item_gift_item_id_from')->unsigned();
$table->integer('item_gift_item_id_to')->unsigned();
$table->integer('item_gift_quantity');
$table->integer('item_gift_min_order');
$table->timestamps();
});
Schema::table('item_gifts', function($table) {
$table->foreign('item_gift_item_id_from')->references('id')->on('items');
$table->foreign('item_gift_item_id_to')->references('id')->on('items');
});
}
您收到此错误,因为您手动创建了项目 table,其中 ID 只是一个整数:
CREATE TABLE items ( id int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
创建新迁移并使其在 item_gists
之前执行:
Schema::create('item_gifts', function (Blueprint $table) {
$table->increments('id');
});
我有 laravel 5.5 个项目 在迁移中我写了这个
Schema::create('item_gifts', function (Blueprint $table) {
$table->increments('id');
$table->string('item_gift_name');
$table->integer('item_gift_item_id_from')->unsigned();
$table->foreign('item_gift_item_id_from')->references('id')->on('items');
$table->integer('item_gift_item_id_to')->unsigned();
$table->foreign('item_gift_item_id_to')->references('id')->on('items');
$table->integer('item_gift_quantity');
$table->integer('item_gift_min_order');
$table->timestamps();
});
但我总是遇到这个错误
In Connection.php line 664:
SQLSTATE[HY000]: General error: 1005 Can't create table `lar
avel`.`#sql-2830_22f` (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table `item_gifts` add con
straint `item_gifts_user_id_foreign` foreign key (`user_id`)
references `items` (`id`))
In Connection.php line 458:
SQLSTATE[HY000]: General error: 1005 Can't create table `lar
avel`.`#sql-2830_22f` (errno: 150 "Foreign key constraint is
incorrectly formed")
我需要的是将 item_gifts 中的 item_gift_item_id_from 与项目中的 id 连接起来 我尝试了很多解决方案,但没有任何效果 谢谢..
试试这个代码:
public function up()
{
Schema::create('item_gifts', function($table) {
$table->increments('id');
$table->string('item_gift_name');
$table->integer('item_gift_item_id_from')->unsigned();
$table->integer('item_gift_item_id_to')->unsigned();
$table->integer('item_gift_quantity');
$table->integer('item_gift_min_order');
$table->timestamps();
});
Schema::table('item_gifts', function($table) {
$table->foreign('item_gift_item_id_from')->references('id')->on('items');
$table->foreign('item_gift_item_id_to')->references('id')->on('items');
});
}
您收到此错误,因为您手动创建了项目 table,其中 ID 只是一个整数:
CREATE TABLE items ( id int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
创建新迁移并使其在 item_gists
之前执行:
Schema::create('item_gifts', function (Blueprint $table) {
$table->increments('id');
});