【问题标题】:errno: 150 Foreign key constraint is incorrectly formederrno: 150 外键约束格式不正确
【发布时间】: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 table item_gifts添加约束item_gifts_item_gift_item_id_from_foreign外键(item_gift_item_id_from)引用itemsid))在Connection.php第458行:SQLSTATE [HY000]:一般错误:1005无法创建表lar avel.@987654332 @ (errno: 150 "外键约束格式不正确")
  • 请显示items 表的迁移。
  • 我没有迁移项目,我只是手动创建表,其中一列作为自动增量 id CREATE TABLE items (id int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
  • user_id 来自哪里?

标签: php mysql laravel migration migrate


【解决方案1】:

试试这个代码:

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'); 
 });

}

【讨论】:

  • 我试过这个并且遇到同样的错误 SQLSTATE[HY000]: General error: 1005 Can't create table lar avel.#sql-2830_25f (errno: 150 "Foreign key constraint is wrongly forms") ( SQL:更改表item_gifts添加约束item_gifts_item_gift_item_id_from_foreign外键(item_gift_item_id_from)引用itemsid))在Connection.php第458行:SQLSTATE [HY000]:一般错误:1005无法创建表lar avel.#sql-2830_25f (errno: 150 "外键约束格式不正确")
【解决方案2】:

您收到此错误,因为您手动创建了 items 表并且 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');
});

【讨论】:

  • 它的签名和自动增量
  • @AwarPullldozer 它不是未签名的。 Laravel 的 increments() 使它成为 unsigned
猜你喜欢
  • 2018-03-02
  • 2020-07-07
  • 1970-01-01
  • 2017-04-13
  • 2018-09-04
  • 2019-09-17
  • 2020-12-16
  • 2019-09-15
相关资源
最近更新 更多