Php (错误号:150“外键约束格式不正确”)在Laravel的多对多关系中
我试图在laravel中创建与Php (错误号:150“外键约束格式不正确”)在Laravel的多对多关系中,php,laravel,eloquent,foreign-keys,eloquent-relationship,Php,Laravel,Eloquent,Foreign Keys,Eloquent Relationship,我试图在laravel中创建与客户和商店链接的多对多,但陷入了这个错误(errno:150“外键约束格式不正确”),仍然没有找到答案 这里是我的客户表 Schema::create('customers', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('shop_id'); $table->string('name'); $table-
客户
和商店
链接的多对多
,但陷入了这个错误(errno:150“外键约束格式不正确”)
,仍然没有找到答案
这里是我的客户
表
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');
});
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');
});
这里是我的商店
表
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');
});
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');
});
我的商店
型号
protected $fillable = ['name'];
public function customer()
{
return $this->belongsToMany(\App\Customer::class);
}
protected $fillable = ['name', 'email', 'phone'];
public function shop()
{
return $this->belongsToMany(\App\Shop::class);
}
我的客户
车型
protected $fillable = ['name'];
public function customer()
{
return $this->belongsToMany(\App\Customer::class);
}
protected $fillable = ['name', 'email', 'phone'];
public function shop()
{
return $this->belongsToMany(\App\Shop::class);
}
有什么帮助吗?感谢您的帮助……检查您的模式-
它应该是商店而不是商店
$table->foreign('shop_id')->references('id')->on('shops');
同样地,顾客不是顾客
$table->foreign('customer_id')->references('id')->on('customers');
检查您的模式-
它应该是商店而不是商店
$table->foreign('shop_id')->references('id')->on('shops');
同样地,顾客不是顾客
$table->foreign('customer_id')->references('id')->on('customers');
给你的便条。不要在表格创建命令中同时使用外部命令。
确保始终使用新的迁移文件在表中添加外键
有时会导致其在迁移时生成错误。。 打开bashshell或PHP风暴终端或CMD
php artisan make:migration foreign_customer_id_at_table_shops --table=shops //you Can use your own migration name what you want
在国外\u客户\u id\u在\u表\u商店迁移文件
向上的
向下
给你的便条。不要在表格创建命令中同时使用外部命令。
确保始终使用新的迁移文件在表中添加外键
有时会导致其在迁移时生成错误。。 打开bashshell或PHP风暴终端或CMD
php artisan make:migration foreign_customer_id_at_table_shops --table=shops //you Can use your own migration name what you want
在国外\u客户\u id\u在\u表\u商店迁移文件
向上的
向下
将外键放在关系中,而不是迁移中
public function customer()
{
return $this->belongsToMany('\App\Customer::class','id','shop_id');
}
public function shop()
{
return $this->belongsToMany('\App\Shop::class','id','customer_id');
}
将外键放在关系中,而不是迁移中
public function customer()
{
return $this->belongsToMany('\App\Customer::class','id','shop_id');
}
public function shop()
{
return $this->belongsToMany('\App\Shop::class','id','customer_id');
}
多对多的关系需要第三个表
pivot
表,而您错过了它。为此,请创建新的迁移。
php artisan make:migration create_customer_shop
无需为数据透视表创建模型
然后,您可以使用类似这样的模式透视表。
数据透视表
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');
});
这两个表shops
和customers
没有任何直接关系,它们只是通过透视表建立关系。注意:确保所有三个表id类型
增量('id')
和所有外键都是$table->integer('shop_id')->无符号()
否则会给您错误的格式错误。多对多的关系需要第三个表pivot
表,您会错过它。为此,请创建新的迁移。
php artisan make:migration create_customer_shop
无需为数据透视表创建模型
然后,您可以使用类似这样的模式透视表。
数据透视表
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');
});
这两个表shops
和customers
没有任何直接关系,它们只是通过透视表建立关系。注意:确保所有三个表id类型
增量('id')
和所有外键都是$table->integer('shop_id')->无符号()代码>否则会给您错误的格式。试试---返回$this->belongTomany('App\Shop','Shop\u id')@CamBoKiDz您是否同时尝试两张国外表格?尝试一下---返回$this->belongtomany('App\Shop','Shop\u id')@CamBoKiDz你是在同时尝试两张桌子吗?