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

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

How can I rename column in laravel using migration?

phpmysqldatabaselaravelmigration

提问by Ariasa

I have columns as mentioned bellow:

我有如下提到的列:

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

}

I have made seeder to stnk table

我已经将播种机用于 stnk 表

Now I want to rename idto id_stnk.
I've added a "doctrine / dbal"in the "composer"and do a composer update.

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

I've made migration php artisan migration:make rename_column.
Then I've added new method to rename_column:

我已经进行了迁移php artisan migration:make rename_column
然后我为 rename_column 添加了新方法:

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

});

And then I've tried to run command php artisan migratebut I got error as mentioned bellow:

然后我尝试运行命令,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)

回答by Laurence

You need to create another migration file - and place it in there:

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

Run

Laravel 4:    php artisan migrate:make rename_stnk_column
Laravel 5:    php artisan make:migration rename_stnk_column

Then inside the new migration file place:

然后在新的迁移文件位置:

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

}

回答by webartisan

first thing you want to do is to create your migration file.

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

Type in your command line

输入你的命令行

php artisan make:migration rename_stk_column --table="YOUR TABLE" --create

After creating the file. Open the new created migration file in your app folder under database/migrations.

创建文件后。在数据库/迁移下的应用程序文件夹中打开新创建的迁移文件。

In your up method insert this:

在你的 up 方法中插入:

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

and in your down method:

并在您的向下方法中:

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

then in your command line just type

然后在你的命令行中输入

php artisan migrate

Then wollah! you have just renamed id to id_stnk. BTW you can use

然后哇!您刚刚将 id 重命名为 id_stnk。顺便说一句,你可以使用

php artisan migrate:rollback

to undo the changes. Goodluck

撤消更改。祝你好运

回答by bmatovu

Renaming Columns (Laravel 5.x)

重命名列(Laravel 5.x)

To rename a column, you may use the renameColumn method on the Schema builder. *Before renaming a column, be sure to add the doctrine/dbaldependency to your composer.json file.*

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

Or you can simply required the package using composer...

或者您可以使用 composer 简单地要求该包...

composer require doctrine/dbal

Source:https://laravel.com/docs/5.0/schema#renaming-columns

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

Note:Use make:migrationand not migrate:makefor Laravel 5.x

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

回答by melih sahin

Follow these steps, respectively for rename column migration file.

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

1-Is there Doctrine/dbal library in your project. If you don't have run the command first

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

composer require doctrine/dbal

2-create update migration file for update old migration file. Warning (need to have the same name)

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

php artisan make:migration update_oldFileName_table

for example my old migration file name: create_users_table update file name should : update_users_table

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

3-update_oldNameFile_table.php

3-update_oldNameFile_table.php

Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});

'from' my old column name and 'to' my new column name

“来自”我的旧列名和“至”我的新列名

4-Finally run the migrate command

4-最后运行迁移命令

php artisan migrate

Source link:laravel document

源码链接:laravel 文档

回答by Stan Smulders

Throwing my $0.02 in here since none of the answers worked, but did send me on the right path. What happened was that a previous foreign constraint was throwing the error. Obvious when you think about it.

把我的 0.02 美元扔在这里,因为没有一个答案奏效,但确实让我走上了正确的道路。发生的事情是之前的外部约束引发了错误。仔细想想就很明显了。

So in your new migration's upmethod, first drop that original constraint, rename the column, then add the constraint again with the new column name. In the downmethod, you do the exact opposite so that it's back to the sold setting.

因此,在您的新迁移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');
    });
}

Hope this saves someone some time in the future!

希望这可以在将来为某人节省一些时间!

回答by jay temp

The above answer is great or if it will not hurt you, just rollback the migration and change the name and run migration again.

上面的答案很好,或者如果它不会伤害您,只需回滚迁移并更改名称并再次运行迁移。

 php artisan migrate:rollback