使用laravel中的迁移删除主键并在数据库中自动增加

Vis*_*l B 3 php laravel artisan

我有主键和自动增量字段的表,我想要新的迁移来删除主键索引,并删除自动增量字段.我怎样才能做到这一点.

我创建了新的迁移

public function up()
{
    Schema::table('tbl_message_read_state', function (Blueprint $table) {

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_message_read_state', function (Blueprint $table) {

        $table->dropPrimary('message_id');
        $table->unsignedInteger('message_id');
    });
}
Run Code Online (Sandbox Code Playgroud)

它给了我错误的命令 [Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'message _id' (SQL: alter table tbl_'message_read_state' add 'message_id' int unsigned not null)

怎么了 ?????

小智 9

Blueprint类提供了dropPrimary方法,允许您删除主键.

public function down()
{
    Schema::table('table', function (Blueprint $table) {
        $table->dropPrimary();
        $table->unsignedInteger('id'); // for removing auto increment

    });
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*dam 5

这对我来说是这样的:

Schema::table('table_name', function (Blueprint $table) {
    // Make AI field `id` unsigned otherwise SQL 
    // will throw error when you try to remove it
    $table->integer('id')->unsigned()->change();

    $table->dropColumn('id');

    // If there was a foreign on message_id, make sure to remove it
    $table->dropForeign('table_name_message_id_foreign');

    $table->dropPrimary('message_id');
    $table->dropColumn('message_id');
}
Run Code Online (Sandbox Code Playgroud)


May*_*eyz 2

删除主键:

$table->dropPrimary( 'id' );
Run Code Online (Sandbox Code Playgroud)

参考