【问题标题】:Auto increment Column id is not starting from 1自动递增列 id 不是从 1 开始
【发布时间】:2017-11-03 00:57:12
【问题描述】:

我有这个迁移:

public function up()
{
    Schema::create('players', function (Blueprint $table) {
        $table->increments('id');
        $table->char('country_code',2);
        $table->integer('unoi_id');
        $table->string('first_name');
        $table->string('last_name');
        $table->char('gender', 1);
        $table->timestamp('birthdate');
        $table->integer('school_id');
        $table->string('school_period');
        $table->string('school_level');
        $table->integer('points');
        $table->timestamps();
    });
    Schema::table('players', function(Blueprint $table){
        $table->foreign('country_code')->references('country_code')->on('countries');
    });
}

当我创建一条新记录时,id 列中的值不是从 1 开始的。它从 321654 之类的东西开始,然后每次递增 1。

我的问题是:如何将默认值设置为 1?

【问题讨论】:

标签: php database laravel migration


【解决方案1】:

你可以使用这样的东西。

 ALTER table <table name> AUTO_INCREMENT = <initial value>

表被截断 AUTO_INCREMENT 值不会重置。在下一次插入时,它会考虑下一个递增值,即使在这种情况下,我们也可以执行上述查询来重置自动递增值

【讨论】:

    【解决方案2】:
    // Your migration here:
    Schema::create('players', function (Blueprint $table) {
        //
    });
    
    //then set auto-increment to 1000
    DB::update("ALTER TABLE players AUTO_INCREMENT = 1000;");
    

    见:Set Auto Increment field start form 1000 in migration laravel 5.1

    【讨论】:

      猜你喜欢
      • 2013-10-27
      • 2012-05-31
      • 2011-12-29
      • 2011-12-29
      • 2017-04-12
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多