Is there a way to set the autoincrement initial value of the primary key on a table in Laravel 4 using Migrations with the Schema Builder?
I want to set the id of a table to start at 100. I know that is possible using pure SQL with ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;
, but I want to maintain database versioning with Laravel Migrations.
Any idea?
I'm afraid Laravel still doesn't have a way to change autoincrement values, but you can create a migration and do in it:
<?php
use Illuminate\Database\Migrations\Migration;
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()
{
}
}
Postgres:
class MyTableMigration extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$statement = "ALTER SEQUENCE my_table RESTART WITH 111111";
DB::unprepared($statement);
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With