Laravel migration and renaming a column

For a Laravel project I created a migration in which I added several attributes to a database table and also renamed an existing attribute. I used the following code in the up() method of the migration:


Schema::table('table', function (Blueprint $table) {
    $table->string('new_attribute')->default('');
    $table->renameColumn('existing_column', 'renamed_column');
});

I ran the migration for my Sqlite test database, but after running my tests it turned out the rename was not executed. However, the new attribute was added to the table.

After some experimenting I found out that you should do column renames in separate calls to the Schema facade. After changing the code in the up() method of my migration to the following everything worked as expected:


Schema::table('table', function (Blueprint $table) {
    $table->string('new_attribute')->default('');
});

Schema::table('table', function (Blueprint $table) {
    $table->renameColumn('existing_column', 'renamed_column');
});

I don’t know if this issue is specific to Sqlite or that it also occurs on other databases such as MySQL or PostreSQL. I also have not spent time looking into the cause of this issue. If you know or have an idea please let me know in the comments 🙂

January 4, 2016

Click Here to Leave a Comment Below

Christopher - May 15, 2016

The documentation states that what you are trying to do is not supported.

From the docs:

“Note: Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported”

Reply
Jan-Henk Gerritsen - May 17, 2016

Thanks for letting me know!

Reply
Leave a Reply: