duanfen1312
2016-08-13 17:59 阅读 92
已采纳

Laravel 5.0 [PDOException] SQLSTATE [HY000]:常规错误:1215无法添加外键约束

Good afternoon. I have a small problem on my console creating a relationship with two tables in laravel 5.0.

This is my product migration table:

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

}

My product model:

<?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%");
    }
}

My category model:

<?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%");
    }
}

My brand model:

<?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%");
    }
}

When I run the migration the following error message is generated:

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

Someone who can help me?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    drn34916 drn34916 2016-08-14 13:04

    Thanks for cooperation, solve the problem through responses:

    aetherg, Hilmi Erdem KEREN

    The problem was the order of the tables to make the migration.

    点赞 评论 复制链接分享

相关推荐