I need to start my ids from 1000 in user table, how could I create migration for this.
My current migration is:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id'); // how can I start this from 1000
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
}
Migration to create table and set its auto-increment value as of Laravel 5.5
DB::statement()
can be used to execute any single SQL statement you need.It should be like this(not tested).
Update
Prashant's method works without problems. But as it is said earlier, don't put
use DB;
in the top of your file.And here are the results after
php artisan migrate
Most tables work with increments incrementing from the next biggest integer.
One can always insert an integer, that is higher than the current autoincrementing index. The autoincrementing index will then automatically follow from that new value +1 up.
So, if you have a freshly minted table your current index is 0, the next key will be 0 + 1 = 1.
What we want is a primary key that starts at 1000, so what we do is insert a record with id value of 999, so the next insert will become 1000.
In code:
and now you have an empty table where the next insert id should be 1000.
Please note that if you have values to seed into the table with id values <
startId
you need to do that before you execute these statements. Otherwise the database will throw an constraint violation error.This should work database agnostic, but if there's a database that does not follow this autoincrement rule i'd love to hear about it.