8

Is there a way to set the autoincrement initial value of the primary key on a table in Laravel 4 using Migrations with the Schema Builder?

I want to set the id of a table to start at 100. I know that is possible using pure SQL with ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;, but I want to maintain database versioning with Laravel Migrations.

Any idea?

4

2 回答 2

23

恐怕 Laravel 仍然无法更改自动增量值,但您可以创建一个迁移并在其中执行:

<?php

use Illuminate\Database\Migrations\Migration;

class MyTableMigration extends Migration {

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

    public function up()
    {
        $statement = "
                        ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;
                    ";

        DB::unprepared($statement);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
    }

}
于 2013-11-12T20:10:29.237 回答
1

Postgres

class MyTableMigration extends Migration {

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

    public function up()
    {
        $statement = "ALTER SEQUENCE my_table RESTART WITH 111111";
        DB::unprepared($statement);
    }

    ...
}
于 2018-02-20T15:27:17.710 回答