0

I've created the database migration below. What I'd like to do is create an accounts table with an id as a primary key. However, I don't want the key to autoincrement starting at 1. Rather, I'd like it to autoincrement starting at 800500.

Is there a way to set the default value of a primary key like this?

I'm currently using Laravel v4.2.11 and sqlite v3.8.3.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAccountsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('accounts', function(Blueprint $table)
    {
        $table->increments('id')->unsigned()->default(800500);
        $table->string('name', 100);
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('accounts');
}

}
Graham Kennedy
  • 736
  • 6
  • 18

1 Answers1

1

The default method in the schema builder

Declare(s) a default value for a column

If you need the increment to start at a given value take a look at this answer. You'd add the query to the migration:

public function up()
{       
    Schema::create('accounts', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name', 100);
        $table->timestamps();
    });
    DB::statement("UPDATE SQLITE_SEQUENCE SET seq = 800500 WHERE name = 'accounts'");
}

There is no 'laravel' way to do this.

Community
  • 1
  • 1
edpaez
  • 1,593
  • 1
  • 12
  • 22
  • Thanks for the quick reply, @edpaez. Unfortunately that didn't do it. When I ran the seed file after running the migration as you've set out, it still returns an ID of 1 for my first record. I should have known better that default is the default for a column and not a initial value. D'oh. – Graham Kennedy Nov 27 '14 at 01:22