php - 错误号 : 150 "Foreign key constraint is incorrectly formed" but check everything I know

标签 php mysql laravel laravel-5 database-migration

问题

运行迁移时,最后一个表的标题中出现错误。

我的数据库是本地MySQL。

错误

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)

已经尝试过

这是我检查的:

  • 父表 (shopping_lists) 在子表 (products) 之前创建。
  • 外键 shopping_list_id 与其引用的列具有相同的类型。
  • shopping_listsproducts 这两个表具有相同的数据库引擎 (InnoDB)。

我已阅读其他答案,但找不到解决方案。

迁移

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

感谢您的帮助,如果需要,请向我询问任何额外信息。

最佳答案

2019_05_13_192700_create_products_table.php 迁移中,onDelete 将该值设置为 null。

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

但是您声明的列不可为空

$table->unsignedBigInteger('shopping_list_id');

尝试通过执行以下操作使该列可为空:

$table->unsignedBigInteger('shopping_list_id')->nullable();

关于php - 错误号 : 150 "Foreign key constraint is incorrectly formed" but check everything I know,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56151924/

相关文章:

php - 使用 .htaccess 拒绝访问目录中的所有 PHP 文件

php - 如何在Laravel中访问%APPDATA%

javascript - 使用axios发送表单数据

php - 具有数据库事务的 Laravel Controller-Model 异常处理结构

php - MySQL查询以获取旅行路线

php - 使用 PHP 在每个用户权限的基础上向 MySQL 数据库中的用户显示菜单项?

php - 存储php mysql ajax搜索引擎结果

MySQL:如何查询列并在同一个表中包含来自相关列的信息?

java - 在 hibernate 中打开新 session 会取消表行

javascript - AJAX、PHP、SQL newsfeed 每次都会出错