简体   繁体   English

如何使用迁移更改laravel 5.3中的现有表列

[英]how to alter an existing table column in laravel 5.3 using migrations

I have this: 我有这个:

$table->integer('role_id')->index()->unsigned()->nullable(); $ table-> integer('role_id')-> index()-> unsigned()-> nullable();

But I want to change it to this: 但我想将其更改为:

$table->integer('role_id')->index()->unsigned()->nullable()->default(3); $ table-> integer('role_id')-> index()-> unsigned()-> nullable()-> default(3);

I had wanted to use this, but the source syntax I could not understand: 我曾经想使用它,但是我不明白的源语法:

php artisan make:migration update_role_id_in_users --table=users PHP工匠make:migration update_role_id_in_users --table = users

I even tried using doctrine/ddbal package and running this: 我什至尝试使用doctrine/ddbal软件包并运行以下命令:

php artisan make:migration modify_role_id_in_users --table=users PHP工匠make:migration Modify_role_id_in_users --table = users

with the migration set up like this: 迁移设置如下:

class ModifyRoleIdInUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->integer('role_id')->index()->unsigned()->nullable()->change();
        });
    }
}

But I get this error when I go migrate: 但是当我进行迁移时,我得到了这个错误:

[Illuminate\\Database\\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_role_id_index' (SQL: alter table 'users' add index 'users_role_id_index'('role_ [Illuminate \\ Database \\ QueryException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'(SQL:alter table'users添加索引'users_role_id_index'('role_
id')) ID'))

[Doctrine\\DBAL\\Driver\\PDOException] SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_role_id_index' [Doctrine \\ DBAL \\ Driver \\ PDOException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_role_id_index' [PDOException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'

How can I alter the column without doing a migrate:refresh 我如何在不进行migrate:refresh情况下更改column migrate:refresh

You can use change() method to do this: 您可以使用change()方法执行此操作:

$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();

Remember to run composer require doctrine/dbal before updating your columns via migrations 在通过迁移更新列之前,请记住运行composer require doctrine/dbal

Hope this helps! 希望这可以帮助!

The solution was to: 解决方案是:

  1. run composer require doctrine/ddbal 运行作曲家需要学说/ ddbal
  2. add the changes I wanted default(3) to the end of the role_id column like this $table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change(); 将我想要的default(3)更改添加到role_id column的末尾,例如$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
  3. The run php artisan migrate 运行php artisan migrate

And that solved it and the table was altered. 这就解决了,桌子也变了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM