[英]Laravel 6: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
[英]Laravel 5.0 [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
下午好。 我的控制台上有一個小問題,它在laravel 5.0中創建了與兩個表的關系。
這是我的產品遷移表:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->string('name', 255);
$table->string('slug');
$table->text('description');
$table->string('extract', 300);
$table->decimal('price', 8, 2);
$table->string('quantity', 300);
$table->string('image', 300);
$table->boolean('visible');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')
->references('id')
->on('categories');
$table->integer('brand_id')->unsigned();
$table->foreign('brand_id')
->references('id')
->on('brands');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('products');
}
}
我的產品型號:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = 'products';
protected $fillable = ['name', 'slug', 'description', 'extract', 'price', 'quantity', 'image', 'visible', 'category_id', 'brand_id'];
// Relation with Category
public function category()
{
return $this->belongsTo('App\Category');
}
// Relation with Brand
public function brand()
{
return $this->belongsTo('App\Brand');
}
/*public function upload()
{
return $this->hasOne('App\Upload');
}*/
//Query para buscador
public function scopeName($query, $name)
{
//dd("scope: " . $name);
$query->where(\DB::raw("CONCAT(name, '', description, '', price, '', quantity)"), "LIKE", "%$name%");
}
}
我的類別模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
protected $fillable = ['name'];
public $timestamps = false;
//Query para buscador
public function scopeName($query, $name)
{
//dd("scope: " . $name);
$query->where(\DB::raw("CONCAT(name)"), "LIKE", "%$name%");
}
}
我的品牌模式:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Brand extends Model
{
protected $table = 'brands';
protected $fillable = ['name'];
public $timestamps = false;
public function products()
{
return $this->hasMany('App\Product');
}
//Query para buscador
public function scopeName($query, $name)
{
//dd("scope: " . $name);
$query->where(\DB::raw("CONCAT(name)"), "LIKE", "%$name%");
}
}
當我運行遷移時,會生成以下錯誤消息:
Migration table created successfully.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `products` add constraint products_brand_id_foreign foreign k
ey (`brand_id`) references `brands` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
有人可以幫助我嗎?
感謝您的合作,請通過答復解決問題:
艾瑟格·希爾米·埃德姆·克倫
問題是要進行遷移的表的順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.