142

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

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

I've made migration php artisan migration:make rename_column.
Then I've added new method to 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:

[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)
Dinesh Patil
  • 615
  • 2
  • 15
  • 37
Ariasa
  • 1,709
  • 3
  • 13
  • 18

10 Answers10

185

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

}
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • i have eddit my question above, look at now.. i have error message in gitbash – Ariasa Oct 23 '14 at 07:58
  • Error 150 is a foreign key restraint. It means you have other tables that reference the `id` on the `stnk` table. – Laurence Oct 23 '14 at 08:47
  • 11
    Also for column renaming to work, you need to require a package that was removed in L5 `"doctrine/dbal": "~2.3"` Without that you get some very obscure errors. It was raised as a bug here https://github.com/laravel/framework/issues/3116 and with an arguably less useful mention in the documentation here http://laravel.com/docs/5.0/schema#renaming-columns – Jason Mar 18 '15 at 19:20
  • Once the migration was done, should the file be deleted and the original create schema updated? Just asking how to keep things clean – orbitory Jun 02 '20 at 20:44
53

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

composer require doctrine/dbal

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

php artisan make:migration update_oldFileName_table

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

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

php artisan migrate

Source link: laravel document

NOTE: This feature supports from laravel 5.x to 8.x version.

UPDATE: This process got even easier after version 9 All you have to do is use the "rename" static method of the "Schema" facade.

use Illuminate\Support\Facades\Schema;
 
Schema::rename($from, $to);

Source link: laravel document-9.x

melih sahin
  • 741
  • 9
  • 12
43

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:

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

php artisan migrate:rollback

to undo the changes. Goodluck

webartisan
  • 536
  • 3
  • 9
21

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.

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

composer require doctrine/dbal

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

Note: Use make:migration and not migrate:make for Laravel 5.x

Dropping Doctrine dependency when renaming database columns starting with Laravel 9 #45258

bmatovu
  • 3,756
  • 1
  • 35
  • 37
  • Just dont use any columns as an ENUM in the table you are trying to edit. Doctrine/dbal does not know what this is..... I had to change the original migration to be correct name at the start and reset whole database. Lucky I was still in development. I would of though Laravel & Co would make this a dependency in composer from the start? – mikoop Apr 09 '17 at 05:58
  • @mikoop It originaly was a dependency from the start, in earlier versions of the framework. But this dependency is too heavy and not commonly used. So it has been removed. (This is the summary; there were lots of discussion before the decision. In fact, community has asked for its removal, and has been heard.) – J. Bruni Jul 02 '17 at 22:39
  • renameColumn was deprecated in doctrine, and currently removed. – Sander Visser Aug 09 '17 at 10:35
15

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.

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.

/**
 * 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!

Stan Smulders
  • 6,079
  • 1
  • 27
  • 23
4

To rename a column, you may use the renameColumn method on the Schema builder.

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

MRMP
  • 303
  • 2
  • 5
2

I know this is an old question, but I faced the same problem recently in Laravel 7 application. To make renaming columns work I used a tip from this answer where instead of composer require doctrine/dbal I have issued composer require doctrine/dbal:^2.12.1 because the latest version of doctrine/dbal still throws an error.

Just keep in mind that if you already use a higher version, this answer might not be appropriate for you.

Strabek
  • 2,391
  • 3
  • 32
  • 39
2

Use a query DB statement if you don't want to use doctrine/dbal package. For example:

public function up()
{
    DB::statement('ALTER TABLE table_name CHANGE from to VARCHAR(200)');
}

public function down()
{
    DB::statement('ALTER TABLE table_name CHANGE to from VARCHAR(200)');
}
Hana Hasanah
  • 145
  • 1
  • 6
0

Just follow bellow steps

Step 1: Install dbal package

composer require doctrine/dbal

Step 2: Create migration and update like bellow

Schema::table('your original table name', function (Blueprint $table) {
   $table->renameColumn('old column name', 'new column name');
});

Step 3: Run artisan command

php artisan migrate

Full Description Links: https://www.webjourney.dev/how-to-rename-table-column-in-laravel-webjourney

Nazmul Hoque
  • 761
  • 9
  • 9
-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
jay temp
  • 1,207
  • 12
  • 11