douwei2966 2019-05-15 14:36
浏览 97
已采纳

错误:150“外键约束错误形成”但检查我所知道的一切

The problem

When running migration, the last table end up with the error in the title.

My database is a local MySQL.

The error

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table shopping_list_develop.#sql-1698_2b (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table products add constraint products_shopping_list_id_foreign foreign key (shopping_list_id) references shopping_lists (id) on delete set null on update cascade)

Already tried

This is what I check:

  • The parent table (shopping_lists) is created before the child table (products).
  • The foreign key shopping_list_id is of the same type of the column it references.
  • The two tables, shopping_lists and products have the same DB Engine (InnoDB).

I've read other answers but can't find a solution.

Migrations

2019_05_13_192170_create_shopping_lists_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Carbon\Carbon;

class CreateShoppingListsTable extends Migration {

    public function up()
    {
        Schema::create('shopping_lists', function(Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->nullable()->default(Carbon::now()->toDateString());
            $table->unsignedBigInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
        });
    }

    public function down()
    {
        Schema::table('shopping_lists', function(Blueprint $table) {
            $table->dropForeign(['user_id']);
        });
        Schema::dropIfExist('shopping_lists');
    }
}

2019_05_13_192700_create_products_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateProductsTable extends Migration {

    public function up()
    {
        Schema::create('products', function(Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('brand')->nullable()->default(null);
            $table->float('price', 6,2)->nullable()->default(null);
            $table->string('note')->nullable()->default(null);
            $table->string('salable_type');
            $table->unsignedBigInteger('salable_id');
            $table->unsignedBigInteger('shopping_list_id');
            $table->timestamps();

            $table->foreign('shopping_list_id')->references('id')->on('shopping_lists')
                        ->onDelete('set null')
                        ->onUpdate('cascade');
        });
    }

    public function down()
    {
        Schema::table('products', function(Blueprint $table) {
            $table->dropForeign(['shopping_list_id']);
        });

        Schema::dropIfExist('products');
    }
}

Thanks for the help and ask me any extra info if you need.

  • 写回答

1条回答 默认 最新

  • douzong5057 2019-05-15 14:43
    关注

    In your 2019_05_13_192700_create_products_table.php migration, onDelete you are setting the value to null.

    $table->foreign('shopping_list_id')->references('id')->on('shopping_lists')
                            ->onDelete('set null')
                            ->onUpdate('cascade');
    

    But the column you have declared is not nullable

    $table->unsignedBigInteger('shopping_list_id');
    

    Try making that column nullable by doing the following instead:

    $table->unsignedBigInteger('shopping_list_id')->nullable();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度