在Laravel 4中设置自动增量初始值 [英] Set autoincrement initial value in Laravel 4

查看:70
本文介绍了在Laravel 4中设置自动增量初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Schema Builder进行迁移,从而在Laravel 4中的表上设置主键的自动递增初始值?

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?

我想将表的ID设置为从100开始.我知道使用纯SQL与ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;是可行的,但是我想通过Laravel Migrations维护数据库版本.

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.

有什么主意吗?

推荐答案

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

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

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

}

这篇关于在Laravel 4中设置自动增量初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆