German Rocha
German Rocha

Reputation: 55

Auto increment Column id is not starting from 1

I have this migration:

public function up()
{
    Schema::create('players', function (Blueprint $table) {
        $table->increments('id');
        $table->char('country_code',2);
        $table->integer('unoi_id');
        $table->string('first_name');
        $table->string('last_name');
        $table->char('gender', 1);
        $table->timestamp('birthdate');
        $table->integer('school_id');
        $table->string('school_period');
        $table->string('school_level');
        $table->integer('points');
        $table->timestamps();
    });
    Schema::table('players', function(Blueprint $table){
        $table->foreign('country_code')->references('country_code')->on('countries');
    });
}

When I am creating a new record the value in the id's column doesn't start on 1. It starts in something like 321654 and then increments by one each time.

My question is: What can I do to set the default value to 1?

Upvotes: 0

Views: 1246

Answers (2)

garrettmills
garrettmills

Reputation: 822

// Your migration here:
Schema::create('players', function (Blueprint $table) {
    //
});

//then set auto-increment to 1000
DB::update("ALTER TABLE players AUTO_INCREMENT = 1000;");

See: Set Auto Increment field start form 1000 in migration laravel 5.1

Upvotes: 1

Ratan Phayade
Ratan Phayade

Reputation: 86

You can use something like this.

 ALTER table <table name> AUTO_INCREMENT = <initial value>

table is truncated AUTO_INCREMENT value wont reset. on next insertion it will consider the next incrementing value, even in those cases we can execute the above query to reset the auto increment value

Upvotes: 1

Related Questions