迁移错误:1005在laravel 5.4

时间:2017-08-15 10:37:09

标签: mysql laravel-5.4 database-migration

我从Laravel 5.4 SQL中收到此错误我试图在我的一个表产品中创建许多外键。请帮我。为什么我收到这个错误?我正在寻找和搜索其他代码,但他们制作多个外键的结构就像我的迁移代码。

    [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `store_db`.`#sql-ca4_1f1` (errno: 150 "Foreign key constrai
  nt is incorrectly formed") (SQL: alter table `products` add constraint `products_product_img_id_foreign` foreign ke
  y (`product_img_id`) references `product_images` (`product_img_id`) on delete cascade)

这是我的迁移代码:   产品表的迁移代码:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('product_id')->unsigned();
        $table->integer('category_id')->unsigned();
        $table->integer('product_img_id')->unsigned();
        $table->string('feature_img');            
        $table->string('product_name');
        $table->text('description');
        $table->double('price',15,2);
        $table->integer('quantity');
        $table->timestamps();

        $table->foreign('category_id')
        ->references('category_id')->on('categories')
        ->onDelete('cascade');

        $table->foreign('product_img_id')
        ->references('product_img_id')->on('product_images')
        ->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('categories');
    // Schema::drop('product_images');
    Schema::drop('products');      
}

product_images表的迁移代码:

public function up()
    {
        Schema::create('product_images', function (Blueprint $table) {
            $table->increments('product_img_id')->unsigned();
            $table->string('product_img');            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('product_images');
    }

类别表的迁移代码:

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('category_id')->unsigned();
            $table->string('name');
            $table->text('description');
            $table->string('category_img');            
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }

有什么建议吗?

0 个答案:

没有答案