在 Laravel 5.1 迁移中设置自增字段从1000开始

33

我需要在用户表中从1000开始设置我的id,我应该如何创建迁移。

我的当前迁移是:

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

1
对于 Laravel 8+,有一个新的修饰符,你可以这样做 $table->id()->from(1000);。我了解这个问题是针对 5.1 的。 - Salam
6个回答

38

应该是这样的(未经测试)。

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

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()
    {
    }
}

更新

//Your migrations here:
Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id')->unsigned();
    $table->integer('qualification_id')->nullable();
    $table->integer('experience_id')->nullable();
});

//then set autoincrement to 1000
//after creating the table
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");

我已经将那行代码粘贴到我的迁移文件中,但是它输出了一个错误:The use statement with non-compound name 'DB' has no effect - Anshul Mishra
1
我认为你在顶部使用了DB(使用DB;),这就是为什么你会得到这个错误,只需将其删除,我已经更新了我的答案,请看一下。 - user5456506
1
这对于所有的数据库来说都不起作用。举个例子,对于SQLite也不起作用。 - abkrim
如果想在SQLite数据库上使用此功能,请使用DB :: update(“UPDATE SQLITE_SEQUENCE SET seq = $ num WHERE name ='$ table'”),其中$num是您的新最后增量编号,$table是表名。http://www.sqlite.org/autoinc.html - abkrim

12

在 Laravel 8 中,你只能将 from() 用于 MySQL / PostgreSQL:

设置自增字段的起始值(MySQL / PostgreSQL

$table->id()->from(...);

这个 startingValue() 方法也有效,但我没有在文档中看到提到它。

$table->id()->startingValue(...);

在 MySQL 内部,它使用:

public function compileAutoIncrementStartingValues(Blueprint $blueprint)
{
    return collect($blueprint->autoIncrementingStartingValues())->map(function ($value, $column) use ($blueprint) {
        return 'alter table '.$this->wrapTable($blueprint->getTable()).' auto_increment = '.$value;
    })->all();
}

$table->id()->from(1000); 语句会将数据ID从1000开始插入。 - Mohamed Raza

8

在Laravel 5.5中进行迁移以创建表并设置其自增值

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('qualification_id')->nullable();
        $table->integer('experience_id')->nullable();
    });

    // Here's the magic
    \DB::statement('ALTER TABLE table_name AUTO_INCREMENT = 1000;');
}

DB::statement() 可以用来执行任何你需要的单个 SQL 语句。


如果我在同一张表中有许多使用增量数据类型的列,例如product_id、counter_id等,我想仅设置counter_id的起始值,这会影响到product_id吗? - Hussam Adil
计数器在表级别上定义。在mysql中,您不能在一个表上有多个AUTO_INCREMENT列,因此您所要求的是不可能的。 - Captain Hypertext

5
大多数表格都使用增量,从下一个最大整数递增。
您总是可以插入一个高于当前自动增量索引的整数。自动增量索引将自动从该新值+1开始。
所以,如果您有一个新创建的表格,您当前的索引是0,下一个键将为0 + 1 = 1。
我们想要的是一个从1000开始的主键,因此我们插入一个id值为999的记录,这样下一个插入就会变成1000。
代码如下:
 $startId = 1000;

 DB::table('users')->insert(['id'=> $startId - 1]);
 DB::table('users')->where('id',$startId - 1)->delete();

现在您有一个空表,下一个插入的id应该是1000。

请注意,如果您有要种子化到id值小于startId的表中的值,则需要在执行这些语句之前进行。否则,数据库将抛出约束违规错误。

这应该是与数据库无关的,但如果有不遵循此自增规则的数据库,我很想了解一下。


是的,决定发布一个与数据库无关的答案,可以在任何数据库引擎上使用,而不是特定于某个数据库的技巧。 - Tschallacka

4
//Your migrations here:
Schema::create('users', function (Blueprint $table) {

    $table->bigIncrements('id')->unsigned();

    $table->integer('qualification_id')->nullable();

    $table->integer('experience_id')->nullable();

 });

 //then set autoincrement to 1000

 //after creating the table

 DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");

我们需要添加前缀表,因此需要替换该行。
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");

通过下面的两行代码:

$prefix = DB::getTablePrefix();
DB::update("ALTER TABLE ".$prefix."users AUTO_INCREMENT = 1000;");

1
感谢。在 Laravel 8 中,您可以直接使用 $table->id()->from(1000);。 - Ahsan Khan

-4

Prashant的方法没有问题。 但正如之前所说,不要将use DB;放在文件顶部。

enter image description here

这是在php artisan migrate之后的结果

And here are the results


1
谢谢您的回答,但我认为您发布了与Prashant相同的答案。 - Anshul Mishra
请详细说明您的答案和假设。 - Szabi Zsoldos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接