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

Posted

技术标签:

【中文标题】Laravel 5.0 [PDOException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束【英文标题】:Laravel 5.0 [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 【发布时间】:2016-12-20 12:05:09 【问题描述】:

下午好。在我的控制台上创建与 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

谁能帮助我?

【问题讨论】:

您确定在运行迁移时先创建类别和品牌表吗? 你好 aethergy,只创建了类别表。 我的意思是,在迁移文件夹中,类别和品牌的迁移应该在产品之前。您不能为不存在的表设置外键。 当您运行 artisan migrate 时,它​​将按照迁移文件的创建顺序创建表。因此,您需要确保类别和品牌迁移在产品迁移之前进行,否则您会收到此错误。 有时,最好将与外键相关的调用移至第三个迁移文件,而不是重命名现有迁移并刷新数据库(尤其是在与团队合作时)。跨度> 【参考方案1】:

感谢合作,通过回复解决问题:

aetherg, Hilmi Erdem KEREN

问题在于进行迁移的表的顺序。

【讨论】:

以上是关于Laravel 5.0 [PDOException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束的主要内容,如果未能解决你的问题,请参考以下文章

无法在 laravel 3 中捕获 PDOException

Laravel 5 PDOException 找不到驱动程序

Laravel 5 PDOException 找不到驱动程序

Laravel:错误 [PDOException]:在 PostgreSQL 中找不到驱动程序?

Laravel 到 SQL Server (sqlsrv)。 [PDOException] 找不到驱动程序

使用 XAMPP 的 Laravel 迁移错误:[PDOException] 找不到驱动程序 [重复]