如何使用迁移重命名laravel中的列?

Ari*_*asa 62 php mysql database migration laravel

我有下面提到的专栏:

public function up()
{
    Schema::create('stnk', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('no_reg', 50)->unique();
        $table->string('no_bpkb', 50)->unique();
        $table->string('nama_pemilik', 100);
        $table->string('alamat');
        $table->string('merk', 50);
        $table->string('tipe', 50);
        $table->string('jenis', 50);
        $table->smallInteger('tahun_pembuatan');
        $table->smallInteger('tahun_registrasi');
        $table->smallInteger('isi_silinder');
        $table->string('no_rangka', 50);
        $table->string('no_mesin', 50);
        $table->string('warna', 50);
        $table->string('bahan_bakar', 50);
        $table->string('warna_tnkb', 50);
        $table->string('kode_lokasi', 50);
        $table->date('berlaku_sampai');
        $table->timestamps();

        $table->index('created_at');
        $table->index('updated_at');
    });

}
Run Code Online (Sandbox Code Playgroud)

我把播种机做成了桌子

现在我想重命名idid_stnk.
我在"作曲家"中添加了一个"教义/ dbal "并做了一个composer update.

我做了迁移php artisan migration:make rename_column.
然后我为rename_column添加了新方法:

Schema::table('stnk', function(Blueprint $table)
{
    $table->renameColumn('id', 'id_stnk');

});
Run Code Online (Sandbox Code Playgroud)

然后我试图运行命令php artisan migrate但是我收到的错误如下所述:

[Ulluminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './my_database/#sql -447_33' to './my_database/stnk' (error: 150) (SQL: ALTER TABLE stnk CHANGE id id_stnk INT UNSIGENED AUTO_INCREMENT NOT NULL)

[PDOException]
SQLSTATE[HY000]: General error: 1025  Error on rename  of './my_database/#sql -447_33' to './my_database/stnk' (error: 150)
Run Code Online (Sandbox Code Playgroud)

Lau*_*nce 89

您需要创建另一个迁移文件 - 并将其放在那里:

Laravel 4:    php artisan migrate:make rename_stnk_column
Laravel 5:    php artisan make:migration rename_stnk_column
Run Code Online (Sandbox Code Playgroud)

然后在新的迁移文件中放置:

class RenameStnkColumn extends Migration
{

    public function up()
    {
        Schema::table('stnk', function(Blueprint $table) {
            $table->renameColumn('id', 'id_stnk');
        });
    }


    public function down()
    {
        Schema::table('stnk', function(Blueprint $table) {
            $table->renameColumn('id_stnk', 'id');
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 同样,为了使列重命名起作用,你需要一个在L5"doctrine/dbal"中删除的包:"~2.3"`如果没有这个,你会得到一些非常模糊的错误.它是在这里提出的一个错误https://github.com/laravel/framework/issues/3116,在这里的文档中提到了一个可以说是不太有用的提示http://laravel.com/docs/5.0/schema#renaming-columns (7认同)
  • 注意Laravel 5现在是`make:migration`而不是`migrate:make` (3认同)

小智 21

您要做的第一件事是创建迁移文件.

输入您的命令行

php artisan make:migration rename_stk_column --table="YOUR TABLE" --create
Run Code Online (Sandbox Code Playgroud)

创建文件后.在database/migrations下的app文件夹中打开新创建的迁移文件.

在你的up方法中插入以下内容:

Schema::table('stnk', function(Blueprint $table)
    {
        $table->renameColumn('id', 'id_stnk');
    });
}
Run Code Online (Sandbox Code Playgroud)

在你的down方法中:

    Schema::table('stnk', function(Blueprint $table)
    {
        $table->renameColumn('id_stnk', 'id);
    });
}
Run Code Online (Sandbox Code Playgroud)

然后在你的命令行中输入

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

那么wollah!你刚刚将id重命名为id_stnk.BTW你可以使用

php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)

撤消更改.祝好运


bma*_*ovu 13

重命名列(Laravel 5.x)

要重命名列,可以在"架构"构建器上使用renameColumn方法.*在重命名列之前,请务必将doctrine/dbal依赖项添加到composer.json文件中.*

或者您可以使用作曲家简单地要求包...

composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)

资料来源: https ://laravel.com/docs/5.0/schema#renaming-columns

注意:使用make:migration而不是migrate:make for Laravel 5.x.


Sta*_*ers 7

因为没有任何答案有效,所以在这里投入0.02美元,但确实让我走上了正确的道路.发生的事情是先前的外部约束引发了错误.当你想到它时显而易见.

因此,在新的迁移up方法中,首先删除原始约束,重命名列,然后使用新列名再次添加约束.在该down方法中,您执行完全相反的操作,以便它返回到已售出的设置.

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('proxy4s', function (Blueprint $table) {
        // Drop it
        $table->dropForeign(['server_id']);

        // Rename
        $table->renameColumn('server_id', 'linux_server_id');

        // Add it
        $table->foreign('linux_server_id')->references('id')->on('linux_servers');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('proxy4s', function (Blueprint $table) {
        // Drop it
        $table->dropForeign(['linux_server_id']);

        // Rename
        $table->renameColumn('linux_server_id', 'server_id');

        // Add it
        $table->foreign('server_id')->references('id')->on('linux_servers');
    });
}
Run Code Online (Sandbox Code Playgroud)

希望这能在将来节省一些时间!


mel*_*hin 7

请分别按照以下步骤操作来重命名列迁移文件。

1-您的项目中是否有教义/ dbal库。如果您没有先运行命令

composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)

2-创建更新迁移文件以更新旧的迁移文件。警告(需要使用相同的名称)

php artisan make:migrate update_oldFileName_table
Run Code Online (Sandbox Code Playgroud)

例如我的旧迁移文件名:create_users_table更新文件名应为:update_users_table

3- update_oldNameFile_table.php

Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
Run Code Online (Sandbox Code Playgroud)

“从”我的旧列名和“到”我的新列名

4-最后运行migrate命令

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

来源链接: laravel文档