在laravel中丢弃foriegn键

时间:2018-03-17 09:39:37

标签: mysql laravel laravel-5.5 laravel-eloquent

我正在尝试使用迁移删除外键。

这是我的代码

public function up()
    {
        Schema::table('tbl_social_media_links', function (Blueprint $table) {
            $table->renameColumn('vchr_link', 'vchr_social_media_link');
            $table->dropColumn('vchr_social_media_name');
            $table->integer('fk_int_business_id')->unsigned()->after('pk_int_sm_id');
            $table->foreign('fk_int_business_id')->references('pk_int_business_id')
                ->on('tbl_business_details')->onDelete('cascade');
            $table->integer('int_social_media_type')->after('fk_int_business_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('tbl_social_media_links', function (Blueprint $table) {
            Schema::disableForeignKeyConstraints();
            $table->string('vchr_social_media_name')->after('pk_int_sm_id');
            $table->dropColumn('fk_int_business_id');
            $table->dropColumn('int_social_media_type');
            $table->renameColumn('vchr_social_media_link', 'vchr_link');
            Schema::enableForeignKeyConstraints();
        });
    }

还试过 $表 - > dropForiegn(' fk_int_business_id&#39);

我一直收到像

这样的错误
General error: 1553 Cannot drop index 'tbl_social_media_links_fk_int_business_id_foreign': needed in a foreign key constraint (
  SQL: alter table `tbl_social_media_links` drop `fk_int_business_id`)

有人可以帮助我让它上班。

我甚至尝试用sql删除它,但它说 Can't DROP 'fk_int_business_id'; check that column/key exists

2 个答案:

答案 0 :(得分:1)

我就是这样做的:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('tbl_social_media_links', function (Blueprint $table) {
        $table->foreign('fk_int_business_id')
            ->references('pk_int_business_id')
            ->on('tbl_business_details')
            ->onDelete('cascade');;
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_social_media_links', function (Blueprint $table) {
        $table->dropForeign(['fk_int_business_id']);
    });
}

请注意,我使用字符串数组作为dropForeign中的参数而不是字符串发送,因为行为不同。使用数组,您可以使用列名,但是使用字符串,则需要使用键名。

但是,我发现,即使删除了外键,索引键仍然存在,所以这样可以解决它:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('tbl_social_media_links', function (Blueprint $table) {
        $table->foreign('fk_int_business_id')
            ->references('pk_int_business_id')
            ->on('tbl_business_details')
            ->onDelete('cascade');;
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_social_media_links', function (Blueprint $table) {
        $index = strtolower('tbl_social_media_links'.'_'.implode('_', ['fk_int_business_id']).'_foreign');
        $index = str_replace(['-', '.'], '_', $index);

        $table->dropForeign($index);
        $table->dropIndex($index);
    });
}

另请注意,在这种情况下,我生成密钥名称而不是使用数组,因为我无法找到任何其他方法来删除仅使用列名称的索引键。

另一方面,请注意您可能需要拆分语句以删除列:

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_social_media_links', function (Blueprint $table) {
        $index = strtolower('tbl_social_media_links'.'_'.implode('_', ['fk_int_business_id']).'_foreign');
        $index = str_replace(['-', '.'], '_', $index);

        $table->dropForeign($index);
        $table->dropIndex($index);
    });

    Schema::table('tbl_social_media_links', function (Blueprint $table) {
        $table->dropColumn('fk_int_business_id');
    });
}

答案 1 :(得分:0)

您不能删除它,直到其他表或列依赖它。

以下是一些类似的问题:MySQL Cannot drop index needed in a foreign key constraint