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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44451230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:06:36  来源:igfitidea点击:

Remove primary key and auto-increment in database using migration in laravel

phplaravelartisan

提问by Vishal B

I have table with primary key and auto-increment field, I want make new migration to drop primary key index and also remove the auto increment field. How can i achieve this.

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

I created new migration as

我创建了新的迁移

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

It gave me error in command as [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)

它给了我命令错误 [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)

Whats Wrong ?????

怎么了 ?????

回答by Rahul

Blueprint class offers dropPrimary methods that allow you to remove primary key.

Blueprint 类提供允许您删除主键的 dropPrimary 方法。

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

    });
}

回答by sturrockad

The dropPrimarymethod only removed the primary key, not the column. You would have to do:

dropPrimary方法只删除了主键,而不是列。你必须这样做:

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

        $table->dropPrimary('message_id');
        $table->dropColumn('message_id');
        $table->unsignedInteger('message_id');
    });
}

Or instead of dropping and re-creating the column, you can use changein Laravel 5.

或者,您可以change在 Laravel 5 中使用,而不是删除和重新创建列。

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

        $table->dropPrimary('message_id');
        $table->integer('message_id')->unsigned()->change();
    });
}

回答by Vishal Varshney

You can try this

你可以试试这个

$table->dropPrimary('id_primary');

回答by Mayank Pandeyz

Drop the primary key:

删除主键:

$table->dropPrimary( 'id' );

Reference

参考

回答by Kimsal San

Use a simple drop column

使用简单的下拉列

$table->dropColumn('id');

$table->dropColumn('id');

回答by Adam

This did it for me:

这为我做到了:

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

回答by Vardges Qeshishyan

https://laravel.com/docs/4.2/schema#dropping-indexes

https://laravel.com/docs/4.2/schema#dropping-indexes

  1. tabel_name
  2. column
  3. index $table->dropPrimary('users_id_primary');
  1. 表名
  2. 柱子
  3. index $table->dropPrimary('users_id_primary');