33

I need to start my ids from 1000 in user table, how could I create migration for this.

My current migration is:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id'); // how can I start this from 1000
        $table->integer('qualification_id')->nullable();
        $table->integer('experience_id')->nullable();
    });
}
Goper Leo Zosa
  • 1,185
  • 3
  • 15
  • 33
Anshul Mishra
  • 1,706
  • 2
  • 15
  • 38
  • 1
    For laravel 8+ there's a [new modifier](https://laravel.com/docs/8.x/migrations#column-modifiers) you can do something like `$table->id()->from(1000);` I understand this question for `5.1` – Salam Sep 30 '20 at 15:32

6 Answers6

38

It should be like this(not tested).

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

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

Update

//Your migrations here:
Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id')->unsigned();
    $table->integer('qualification_id')->nullable();
    $table->integer('experience_id')->nullable();
});

//then set autoincrement to 1000
//after creating the table
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
myckhel
  • 800
  • 3
  • 15
  • 29
  • I have pasted that line under my migration, it output an error: `The use statement with non-compound name 'DB' has no effect` – Anshul Mishra Dec 10 '15 at 07:47
  • 1
    I think you are using DB at the top (use DB;) that is why you are getting this error, just remove this, and I have updated my answer have a look. –  Dec 10 '15 at 08:12
  • 1
    This not work for all databases. Example, not work for sqlite. – abkrim Nov 23 '16 at 17:35
  • If like use this on sqlite database, use `DB::update("UPDATE SQLITE_SEQUENCE SET seq = $num WHERE name = '$table'") ` where $num it's your new last increment number, and $table it's table. http://www.sqlite.org/autoinc.html – abkrim Nov 23 '16 at 17:49
12

In Laravel 8 you can use from() only for MySQL / PostgreSQL:

Set the starting value of an auto-incrementing field (MySQL / PostgreSQL)

$table->id()->from(...);

This startingValue() method also works but I didn't see this mentioned anywhere in the documentation.

$table->id()->startingValue(...);

Under the hood for mysql it uses:

public function compileAutoIncrementStartingValues(Blueprint $blueprint)
{
    return collect($blueprint->autoIncrementingStartingValues())->map(function ($value, $column) use ($blueprint) {
        return 'alter table '.$this->wrapTable($blueprint->getTable()).' auto_increment = '.$value;
    })->all();
}
jewishmoses
  • 1,069
  • 2
  • 11
  • 16
8

Migration to create table and set its auto-increment value as of Laravel 5.5

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('qualification_id')->nullable();
        $table->integer('experience_id')->nullable();
    });

    // Here's the magic
    \DB::statement('ALTER TABLE table_name AUTO_INCREMENT = 1000;');
}

DB::statement() can be used to execute any single SQL statement you need.

Captain Hypertext
  • 2,446
  • 4
  • 27
  • 36
  • what if I have many colums with increments datatype in the same table for example product_id , counter_id and I want to set the starting value for counter_id only , is this will affect product_id as well ? – Hussam Adil Sep 04 '19 at 11:33
  • The counter is defined at the table level. You cannot have multiple AUTO_INCREMENT columns on one table in mysql, so what you are asking is not possible. – Captain Hypertext Sep 04 '19 at 11:47
5

Most tables work with increments incrementing from the next biggest integer.

One can always insert an integer, that is higher than the current autoincrementing index. The autoincrementing index will then automatically follow from that new value +1 up.

So, if you have a freshly minted table your current index is 0, the next key will be 0 + 1 = 1.

What we want is a primary key that starts at 1000, so what we do is insert a record with id value of 999, so the next insert will become 1000.

In code:

 $startId = 1000;

 DB::table('users')->insert(['id'=> $startId - 1]);
 DB::table('users')->where('id',$startId - 1)->delete();

and now you have an empty table where the next insert id should be 1000.

Please note that if you have values to seed into the table with id values < startId you need to do that before you execute these statements. Otherwise the database will throw an constraint violation error.

This should work database agnostic, but if there's a database that does not follow this autoincrement rule i'd love to hear about it.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Yea, decided to post a database agnostic answer that should be useable on any database engine, instead of the database specific hacks. – Tschallacka Aug 22 '17 at 09:23
4
//Your migrations here:
Schema::create('users', function (Blueprint $table) {

    $table->bigIncrements('id')->unsigned();

    $table->integer('qualification_id')->nullable();

    $table->integer('experience_id')->nullable();

 });

 //then set autoincrement to 1000

 //after creating the table

 DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");

We need add Prefix table. So we need replace the line

DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");

by 2 lines below:

$prefix = DB::getTablePrefix();
DB::update("ALTER TABLE ".$prefix."users AUTO_INCREMENT = 1000;");
Au Nguyen
  • 655
  • 4
  • 12
Bang T
  • 41
  • 1
-4

Prashant's method works without problems. But as it is said earlier, don't put use DB; in the top of your file.

enter image description here

And here are the results after php artisan migrate

And here are the results

Szabi Zsoldos
  • 351
  • 6
  • 17