Set autoincrement initial value in Laravel 4

放肆的年华 提交于 2019-12-08 17:05:18

问题


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?


回答1:


I'm afraid Laravel still doesn't have a way to change autoincrement values, but you can create a migration and do in it:

<?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()
    {
    }

}



回答2:


Postgres:

class MyTableMigration extends Migration {

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

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

    ...
}


来源:https://stackoverflow.com/questions/19938846/set-autoincrement-initial-value-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!