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');
}

}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.