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 id
to id_stnk
.
I've added a "doctrine / dbal" in the "composer" and do a composer update
.
现在我想将id重命名为id_stnk。我在“作曲家”中添加了一个“doctrine / dbal”,并做了一个编写器更新。
I've made migration php artisan migration:make rename_column
.
Then I've added new method to rename_column:
我已经进行了迁移php artisan迁移: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 migrate
but I got error as mentioned bellow:
然后我试着运行命令php artisan迁移但是我得到了错误如下面提到的:
[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)
5 个解决方案
#1
34
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');
});
}
}
#2
13
first thing you want to do is to create your migration file.
首先要做的是创建迁移文件。
Type in your command line
输入命令行
php artisan migrate:make rename_stk_column --table="YOUR TABLE" --create
After creating the file. Open the new created migration file in your app folder under database/migrations.
在创建该文件。在数据库/迁移下的app文件夹中打开新创建的迁移文件。
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
然后wollah !您刚刚将id重命名为id_stnk。顺便说一句你可以使用
php artisan migrate:rollback
to undo the changes. Goodluck
撤销更改。古德勒克
#3
2
Renaming Columns (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/dbal dependency to your composer.json file.*
要重命名列,可以在模式构建器上使用renameColumn方法。*在重命名列之前,一定要将doctrine/dbal依赖项添加到编写器中。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:migration and not migrate:make for Laravel 5.x
注意:Use make:migration and not migration:make for Laravel 5.x
#4
1
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
#5
1
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 up
method, first drop that original constraint, rename the column, then add the constraint again with the new column name. In the down
method, 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!
希望这能在将来节省一些时间!
#1
34
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');
});
}
}
#2
13
first thing you want to do is to create your migration file.
首先要做的是创建迁移文件。
Type in your command line
输入命令行
php artisan migrate:make rename_stk_column --table="YOUR TABLE" --create
After creating the file. Open the new created migration file in your app folder under database/migrations.
在创建该文件。在数据库/迁移下的app文件夹中打开新创建的迁移文件。
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
然后wollah !您刚刚将id重命名为id_stnk。顺便说一句你可以使用
php artisan migrate:rollback
to undo the changes. Goodluck
撤销更改。古德勒克
#3
2
Renaming Columns (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/dbal dependency to your composer.json file.*
要重命名列,可以在模式构建器上使用renameColumn方法。*在重命名列之前,一定要将doctrine/dbal依赖项添加到编写器中。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:migration and not migrate:make for Laravel 5.x
注意:Use make:migration and not migration:make for Laravel 5.x
#4
1
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
#5
1
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 up
method, first drop that original constraint, rename the column, then add the constraint again with the new column name. In the down
method, 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!
希望这能在将来节省一些时间!