【发布时间】:2018-06-19 16:13:09
【问题描述】:
我有 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 与 items 中的 id 连接起来 我尝试了很多解决方案,但没有任何效果 谢谢..
【问题讨论】:
-
该错误是关于
user_id的,而您在显示的迁移中没有它。 -
SQLSTATE[HY000]: 一般错误: 1005 无法创建表
lar avel.#sql-2830_24a(errno: 150 "外键约束格式不正确") (SQL: alter tableitem_gifts添加约束item_gifts_item_gift_item_id_from_foreign外键(item_gift_item_id_from)引用items(id))在Connection.php第458行:SQLSTATE [HY000]:一般错误:1005无法创建表lar avel.@987654332 @ (errno: 150 "外键约束格式不正确") -
请显示
items表的迁移。 -
我没有迁移项目,我只是手动创建表,其中一列作为自动增量 id CREATE TABLE
items(idint(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1 ; -
user_id 来自哪里?
标签: php mysql laravel migration migrate