在 Laravel 4 中设置自动增量初始值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19938846/
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-09-14 08:39:07  来源:igfitidea点击:

Set autoincrement initial value in Laravel 4

phplaravellaravel-4

提问by arielcr

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?

有没有办法使用架构生成器的迁移在 Laravel 4 中的表上设置主键的自动增量初始值?

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.

我想将表的 id 设置为从 100 开始。我知道使用纯 SQL with 是可能的ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;,但我想使用 Laravel Migrations 维护数据库版本控制。

Any idea?

任何的想法?

回答by Antonio Carlos Ribeiro

I'm afraid Laravel still doesn't have a way to change autoincrement values, but you can create a migration and do in it:

恐怕 Laravel 仍然无法更改自动增量值,但您可以创建迁移并在其中执行:

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

}

回答by Kamil Kie?czewski

Postgres:

邮政

class MyTableMigration extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */

    public function up()
    {
        $statement = "ALTER SEQUENCE my_table RESTART WITH 111111";
        DB::unprepared($statement);
    }

    ...
}