mysql - 在 Laravel 中删除外键

标签 mysql laravel laravel-5.5 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();
        });
    }

也尝试过 $table->dropForiegn('fk_int_business_id');

我不断收到类似错误

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 删除它,但它说 无法删除“fk_int_business_id”;检查列/键是否存在

最佳答案

这就是我要做的:

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

关于mysql - 在 Laravel 中删除外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49334545/

相关文章:

php - 在变量中使用破折号 (-) 时出现 PDO 错误

mysql - 使用变量作为名称删除外键

php - 使用 Laravel Eloquent 在 MySQL 列别名中添加空格

laravel - session.php 中 laravel 生命周期配置变量的最大可能值是多少

php - 如何将我的生产应用程序链接到 Laravel 5 中的生产数据库?

php - Laravel 5.5 - 查询多个条件,但仅当 POST 数据等于 1 时

ubuntu - 在 Windows 文件服务器上存储从 Ubuntu 上传的文件

php - CodeIgniter 将数据插入数据库并在字符串中添加引号

java - 实际上没有插入到 MySQL 中,但是自动增量字段在增加?

php - 拉拉维尔 : How to submit a registration using a bootstrap Modal?