duanfen1312 2016-08-13 17:59
浏览 109
已采纳

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services