Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我是否可以在删除所述类别时将图像的类别id更改为空?_Php_Laravel_Eloquent - Fatal编程技术网

Php 我是否可以在删除所述类别时将图像的类别id更改为空?

Php 我是否可以在删除所述类别时将图像的类别id更改为空?,php,laravel,eloquent,Php,Laravel,Eloquent,我有两个表-图像和类别。images表有一个名为category\u id的列,该列获取分配给该图像的类别的id。我尝试将category\u id设置为一个外键,该外键将在cascade中删除,但没有成功,可能我没有正确执行 Schema::create('images', function (Blueprint $table) { $table->increments('id'); $table->string('name');

我有两个表-
图像
类别
images
表有一个名为
category\u id
的列,该列获取分配给该图像的类别的id。我尝试将
category\u id
设置为一个外键,该外键将在cascade中删除,但没有成功,可能我没有正确执行

    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')
              ->references('id')
              ->on('users')
              ->onDelete('cascade');
        $table->integer('category_id')->nullable()->default(null)->unsigned();
        $table->foreign('category_id')
              ->references('id')
              ->on('categories')
              ->onDelete('cascade');
        $table->string('image_file_name');
        $table->timestamps();
        $table->engine = 'InnoDB';
    });
我收到的错误是:

SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL) :alter table
images
添加约束
images\u category\u id\u foreign
foreign 键(
category\u id
)引用删除级联上的
categories
id


在解决了迁移问题之后,我发现->onDelete('cascade')不是我想要的。我只想在删除图像时将category_id设置为NULL,而不是同时删除图像。

迁移文件的顺序会影响外键,因此处理此类问题有两种方法:

  • 按特定顺序创建表,因此必须在相关表之后创建外键

  • 为外键创建新迁移,但请确保在两个表之后创建此迁移文件


关于第二个问题: 调用
onDelete('cascade')将删除与您删除的类别相关的任何图像

现在,如果您想将外键类别id设置为null,只需将其替换为
->onDelete('set null')


希望这有帮助。

此迁移没有任何错误。我认为您应该检查categories表是否存在,categories表中的id列是否为无符号整数(increment())。 我在我的系统上测试了这个迁移,它运行得非常好

    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
        $table->engine = 'InnoDB';
    });

    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->integer('category_id')->nullable()->default(null)->unsigned();
        $table->foreign('category_id')
            ->references('id')
            ->on('categories')
            ->onDelete('cascade');
        $table->string('image_file_name');
        $table->timestamps();
        $table->engine = 'InnoDB';
    });

它是否抛出了任何异常或只是不工作?哦,我收到了SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table
images
add constraint
images\u category\u id\u foreign
外键(
category\u id
)引用
categories
id
)关于删除级联)您是先创建categories表的吗?我使用php artisan migrate,所以我不确定创建表的顺序。我可以先创建类别吗?通常迁移文件名以日期开头,所以它必须按照运行顺序进行排序,所以只需检查文件的顺序就可以了。谢谢