在sraite的Laravel Migration中设置主键的默认值

时间:2014-11-26 21:39:04

标签: laravel-4 sqlite

我在下面创建了数据库迁移。我想要做的是创建一个id为主键的帐户表。但是,我不希望钥匙从1开始自动增量。相反,我希望它从800500开始自动增量。

有没有办法像这样设置主键的默认值?

我目前正在使用Laravel v4.2.11和sqlite v3.8.3。

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAccountsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('accounts', function(Blueprint $table)
    {
        $table->increments('id')->unsigned()->default(800500);
        $table->string('name', 100);
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('accounts');
}

}

1 个答案:

答案 0 :(得分:2)

schema builder

中的default方法
  

声明列的默认值

如果您需要以给定值开始增量,请查看this答案。 您将查询添加到迁移中:

public function up()
{       
    Schema::create('accounts', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name', 100);
        $table->timestamps();
    });
    DB::statement("UPDATE SQLITE_SEQUENCE SET seq = 800500 WHERE name = 'accounts'");
}

没有' laravel'这样做的方式。