Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set autoincrement initial value in Laravel 4

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?

like image 952
arielcr Avatar asked Nov 12 '13 20:11

arielcr


2 Answers

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

}
like image 73
Antonio Carlos Ribeiro Avatar answered Oct 01 '22 18:10

Antonio Carlos Ribeiro


Postgres:

class MyTableMigration extends Migration {

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

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

    ...
}
like image 34
Kamil Kiełczewski Avatar answered Oct 01 '22 17:10

Kamil Kiełczewski