php 在迁移 laravel 5.1 中设置自动增量字段从 1000 开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34196045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:49:47  来源:igfitidea点击:

Set Auto Increment field start from 1000 in migration laravel 5.1

phpmysqlmigrationlaravel-5.1

提问by Anshul Mishra

I need to start my ids from 1000 in user table, how could I create migration for this.

我需要从用户表中的 1000 开始我的 id,我怎么能为此创建迁移。

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

回答by

It should be like this(not tested).

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

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

Update

更新

//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;");

回答by Captain Hypertext

Migration to create table and set its auto-increment value as of Laravel 5.5

从 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()can be used to execute any single SQL statement you need.

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

回答by Tschallacka

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.

人们总是可以插入一个整数,该整数高于当前的自动递增索引。然后自动递增的索引将自动从该新值 +1 开始。

So, if you have a freshly minted table your current index is 0, the next key will be 0 + 1 = 1.

因此,如果您有一个新创建的表,您当前的索引是 0,那么下一个键将是 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.

我们想要的是一个从1000开始的主键,所以我们要做的是插入一条id值为999的记录,这样下次插入就会变成1000。

In code:

在代码中:

 $startId = 1000;

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

and now you have an empty table where the next insert id should be 1000.

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

Please note that if you have values to seed into the table with id values < startIdyou need to do that beforeyou execute these statements. Otherwise the database will throw an constraint violation error.

请注意,如果您有值以 id values < 进入表,则startId需要执行这些语句之前执行此操作。否则数据库将抛出约束冲突错误。

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.

这应该与数据库无关,但如果有一个不遵循此自动增量规则的数据库,我很想听听它。

回答by Bang T

//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;");

We need add Prefix table. So we need replace the line

我们需要添加前缀表。所以我们需要更换线路

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

by 2 lines below:

通过以下 2 行:

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

回答by Szabi Zsoldos

Prashant's method works without problems. But as it is said earlier, don't put use DB;in the top of your file.

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

enter image description here

在此处输入图片说明

And here are the results after php artisan migrate

这是之后的结果 php artisan migrate

And here are the results

这是结果