【问题标题】:(errno: 150 "Foreign key constraint is incorrectly formed") in many-to-many relationship in Laravel(errno:150“外键约束不正确”)在Laravel中的多对多关系中
【发布时间】:2020-02-10 02:31:01
【问题描述】:

我尝试在 laravel 中创建 many-to-manycustomershop 的链接,但卡在此错误 (errno: 150 "Foreign key constraint is incorrectly formed") 中,但仍然无法弄清楚。

这是我的customers

Schema::create('customers', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('shop_id');
  $table->string('name');
  $table->string('email');
  $table->string('phone');
  $table->timestamps();

  $table->foreign('shop_id')->references('id')->on('shop');
});

这是我的shops

Schema::create('shops', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('customer_id');
  $table->string('name');
  $table->timestamps();

  $table->foreign('customer_id')->references('id')->on('customer');
});  

我的Shop 模特

protected $fillable = ['name'];

public function customer()
{
  return $this->belongsToMany(\App\Customer::class);
}

我的Customer 模特

protected $fillable = ['name', 'email', 'phone'];

public function shop()
{
  return $this->belongsToMany(\App\Shop::class);
}

有什么帮助吗?在此先感谢....

【问题讨论】:

    标签: php laravel eloquent foreign-keys eloquent-relationship


    【解决方案1】:

    检查您的架构 - 它应该是商店而不是商店......

    $table->foreign('shop_id')->references('id')->on('shops');
    

    同样的客户不是客户......

    $table->foreign('customer_id')->references('id')->on('customers');
    

    【讨论】:

    • 试试看 --- return $this->belongsToMany('App\Shop', 'shop_id');
    • @CamBoKiDz 你要同时尝试 2 张外国桌子吗?
    【解决方案2】:

    请注意。请勿在建表命令中同时使用外部命令。
    确保始终使用新的迁移文件来添加外键的内表。
    有时会导致它在迁移时生成错误.. 打开你的 bash shell 或 PHP Storm Terminal 或 CMD

    php artisan make:migration foreign_customer_id_at_table_shops --table=shops //you Can use your own migration name what you want
    

    在 foreign_customer_id_at_table_shops 迁移文件中

    向上

    $table->foreign('customer_id')->references('id')->on('customers');
    

    向下

    $table->dropForeign(['customer_id']);
    

    【讨论】:

      【解决方案3】:

      将外键放在关系中而不是迁移中。

       public function customer()
       {
        return $this->belongsToMany('\App\Customer::class','id','shop_id');
       }
      
       public function shop()
       {
         return $this->belongsToMany('\App\Shop::class','id','customer_id');
       }
      

      【讨论】:

        【解决方案4】:

        多对多的关系需要第三张表pivot你错过的表。
        为此创建新的迁移。
        php artisan make:migration create_customer_shop

        无需为数据透视表创建模型

        然后你 Schema 数据透视表是这样的。
        数据透视表

        Schema::create('cutomer_shop', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('customer_id')->unsigned();
          $table->integer('shop_id')->unsigned();
          $table->timestamps();
        
          $table->foreign('customer_id')->references('id')->on('customers');
          $table->foreign('shop_id')->references('id')->on('shops');
        });  
        

        shopscustomers 这两个表没有任何直接关系,它们仅通过数据透视表建立关系。
        注意:确保所有三个表 id 类型为 increments('id')并且所有外键都是$table->integer('shop_id')->unsigned(); 否则会给你不正确的格式错误。

        【讨论】:

          猜你喜欢
          • 2021-07-22
          • 2017-04-13
          • 2019-09-17
          • 2020-12-16
          • 1970-01-01
          • 1970-01-01
          • 2019-07-14
          • 2021-12-30
          相关资源
          最近更新 更多