Recently, I was creating a unit test for some new functionality. This additional functionality involved altering the name of a column on an existing database table as well as adding a new column within the same migration:
Schema::table('existing_table', function (Blueprint $table) {
$table->renameColumn('old_name', 'new_name');
$table->string('new_column');
});
I ran into an issue where an error kept being thrown with the following looking something like:
[..] Table 'existing_table' has no column named 'new_column'
I find situations like this, especially when involving sqlite, can be solved by separating table modifications into separate operations:
Schema::table('existing_table', function (Blueprint $table) {
$table->renameColumn('old_name', 'new_name');
});
Schema::table('existing_table', function (Blueprint $table) {
$table->string('new_column');
});
Once this change was made, all tests ran as expected.