Laravel中如何解除Auto-incremented Integer Primary Key?


本來應該係過幾日先寫,但係更新緊個table發現咗呢個問題,立即過黎寫低先...

喺Laravel裡面,migrations係用黎做類似database版嘅version control,你可以用諸如php artisan migrate或者php artisan migrate:rollback去更新或者還原database入面嘅table structure。由於呢幾日喺度搞緊User Management嗰邊嘅table,所以就喺rollback嗰陣遇上咗問題。

問題code:
/**
 * Run the migrations.
 *
 * @return void
 */

public function up()
{
    Schema::table('dummy' function(Blueprint $table) {
        $table->increments('id')->first();
    });
}

/**
 * Reverse the migrations.
 * 
 * @return void
 */

public function down()
{
    Schema::table('dummy', function(Blueprint $table) {
        $table->dropPrimary(['id']);
        $table->dropColumn('id');
    });
}

喺更新嘅位我加咗一個會Auto-increment嘅id欄位並作為primary key存在,而當我要還原嘅時候應該係要首先將id嘅primary key屬性移除先再去將成個欄位刪走。不過好不幸地,當我rollback嗰陣出現咗以下嘅錯誤:

SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

於是就諗起我要首先將個auto-increment特性移除咗先,但係Laravel嘅說明文檔無一個modifier係有呢個功能。結果喺上網搵完resource後發現原來我可以咁樣...

public function down()
{
    Schema::table('dummy', function(Blueprint $table) {
        $table->integer('id')->unsigned()->change();
        $table->dropPrimary(['id']);
        $table->dropColumn('id');
    });
}

利用change()嘅method將id欄位嘅unsign特性移走,咁就會順手無咗auto-increment嘅效果。之後就可以成功rollback啦。

Powered by Half-moon Production.

Copyright © 2011-2020 by 泣雨家的雜物房. Version 1.5.2.

Laravel中如何解除Auto-incremented Integer Primary Key?


本來應該係過幾日先寫,但係更新緊個table發現咗呢個問題,立即過黎寫低先...

喺Laravel裡面,migrations係用黎做類似database版嘅version control,你可以用諸如php artisan migrate或者php artisan migrate:rollback去更新或者還原database入面嘅table structure。由於呢幾日喺度搞緊User Management嗰邊嘅table,所以就喺rollback嗰陣遇上咗問題。

問題code:
/**
 * Run the migrations.
 *
 * @return void
 */

public function up()
{
    Schema::table('dummy' function(Blueprint $table) {
        $table->increments('id')->first();
    });
}

/**
 * Reverse the migrations.
 * 
 * @return void
 */

public function down()
{
    Schema::table('dummy', function(Blueprint $table) {
        $table->dropPrimary(['id']);
        $table->dropColumn('id');
    });
}

喺更新嘅位我加咗一個會Auto-increment嘅id欄位並作為primary key存在,而當我要還原嘅時候應該係要首先將id嘅primary key屬性移除先再去將成個欄位刪走。不過好不幸地,當我rollback嗰陣出現咗以下嘅錯誤:

SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

於是就諗起我要首先將個auto-increment特性移除咗先,但係Laravel嘅說明文檔無一個modifier係有呢個功能。結果喺上網搵完resource後發現原來我可以咁樣...

public function down()
{
    Schema::table('dummy', function(Blueprint $table) {
        $table->integer('id')->unsigned()->change();
        $table->dropPrimary(['id']);
        $table->dropColumn('id');
    });
}

利用change()嘅method將id欄位嘅unsign特性移走,咁就會順手無咗auto-increment嘅效果。之後就可以成功rollback啦。