Problem
As you can see, I have the following columns:
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 made a seeder for the stnk table.
I’d like to rename id to id stnk now. I’ve added “doctrine / dbal” to the “composer” and updated the composer.
I used the php artisan migration:make rename column command. Then I added a new rename column method:
Schema::table('stnk', function(Blueprint $table)
{
$table->renameColumn('id', 'id_stnk');
});
Then I tried to run the command php artisan migrate, but I got the following error:
[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)
Asked by Ariasa
Solution #1
You must generate a new migration file and place it in the following location:
Run
Laravel 4: php artisan migrate:make rename_stnk_column
Laravel 5: php artisan make:migration rename_stnk_column
Then, within the new migration file, add the following:
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');
});
}
}
Answered by Laurence
Solution #2
The first thing you should do is make a migration file.
Enter your command line here.
php artisan make:migration rename_stk_column --table="YOUR TABLE" --create
Following the creation of the file. In your app’s database/migrations folder, open the newly created migration file.
Add the following to your up method:
Schema::table('stnk', function(Blueprint $table)
{
$table->renameColumn('id', 'id_stnk');
});
}
in addition to your down method:
Schema::table('stnk', function(Blueprint $table)
{
$table->renameColumn('id_stnk', 'id);
});
}
then enter in the following command in your command prompt:
php artisan migrate
Then hallelujah! You’ve simply changed the name of id to id stnk. You may, by the way, use
php artisan migrate:rollback
to reverse the changes Goodluck
Answered by webartisan
Solution #3
To rename a column migration file, follow the procedures below.
1- Is your project using the Doctrine/dbal library? If you haven’t already, run the command.
composer require doctrine/dbal
2- Create a new migration file to replace the old one. Caution! (need to have the same name)
php artisan make:migration update_oldFileName_table
For instance, here’s the name of my old migration file: update users table should be the name of the update file for create users table.
3- update_oldNameFile_table.php
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
‘from’ and ‘to’ are the names of my old and new columns, respectively.
4- Last but not least, run the migrate command.
php artisan migrate
laravel document is the source link.
NOT: This functionality is compatible with Laravel 5.x and 8.x.
Answered by melih sahin
Solution #4
The renameColumn method on the Schema builder can be used to rename a column. Make sure to include the doctrine/dbal dependency in your composer.json file before renaming a column.
Alternatively, you can just use composer to require the package…
composer require doctrine/dbal
Source: https://laravel.com/docs/5.0/schema#renaming-columns
For Laravel 5.x, use make:migration instead of migrate:make.
Answered by bmatovu
Solution #5
I’m throwing in my $0.02 because none of the answers worked for me, but they did lead me in the correct direction. The problem was caused by a previous foreign constraint that was producing an error. When you think about it, it’s self-evident.
So, in the up method of your new migration, remove the existing constraint, rename the column, and then re-add the constraint with the new column name. In the down technique, you reverse the steps to return to the sold position.
/**
* 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');
});
}
I hope this helps someone in the future save time!
Answered by Stan Smulders
Post is based on https://stackoverflow.com/questions/26522292/how-can-i-rename-column-in-laravel-using-migration