2016-08-13 117 views
0

下午好。我在控制檯上創建了一個與laravel 5.0中的兩個表關係的小問題。Laravel 5.0 [PDOException] SQLSTATE [HY000]:常規錯誤:1215無法添加外鍵約束

這是我的產品遷移表:

<?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 

有人誰可以幫我嗎?

+0

是否確定在運行遷移時在產品表格之前創建了分類和品牌表格? – aethergy

+0

你好乙醚,只有類別表被創建。 –

+0

我的意思是,在遷移文件夾中,遷移類別和品牌應該放在產品之前。您不能爲不存在的表設置外鍵。 – aethergy

回答

0

謝謝合作,通過答覆解決的問題:

aetherg,希爾米·埃德姆KEREN

的問題是表的順序進行遷移。