0

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?

German Rocha
  • 55
  • 1
  • 14
  • 2
    https://stackoverflow.com/questions/8923114/how-to-reset-auto-increment-in-mysql laravel has a migrate fresh command: https://laravel-news.com/migrate-fresh – Matt Jun 01 '17 at 18:54
  • migrate:fresh is Laravel 5.5. The current stable version of Laravel is 5.4 – Brad Jun 01 '17 at 19:06
  • Thank you. I never thought about doing it directly with a mysql command. – German Rocha Jun 01 '17 at 19:29

2 Answers2

1
// 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

garrettmills
  • 660
  • 6
  • 13
1

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