Rename and change column definition at once #44912
-
On MySQL, It's possible to rename and change column definition in one clause. Lets assume we have an Schema::table('users', function (Blueprint $table) {
$table->char('foo')->nullable()->change();
$table->renameColumn('foo', 'bar');
}); This compiles to:
As you see, the first clause changes the type as expected but not name, then the second clause changes the name but reverts the type definition. The compiled query won't change if you reorder the blueprint commands. any solutions? PS: I know I can use 2 separate |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
You need to install and use DBAL I think. |
Beta Was this translation helpful? Give feedback.
-
I have had another look at the API definitions with the hope that you might be able to chain them, but renameColumn returns a Fluent object, so that is not possible. I assume that you have tried doing it the other way around i.e. Schema::table('users', function (Blueprint $table) {
$table->renameColumn('foo', 'bar');
$table->char('bar')->nullable()->change();
}); What SQL DDL does this produce? Something like: ALTER TABLE users CHANGE foo bar INT NOT NULL
ALTER TABLE users CHANGE bar bar CHAR(255) DEFAULT NULL If the above doesn't work, then I suspect (without having looked at the code) that each ALTER TABLE users CHANGE foo bar CHAR(255) DEFAULT NULL then this would have to be done in one-line of PHP i.e. something like: Schema::table('users', function (Blueprint $table) {
$table->char('bar')->nullable()->change('foo');
}); The API for I will take a look this evening at how difficult this would be, and if not too difficult I will create a PR for it. :-) |
Beta Was this translation helpful? Give feedback.
-
I would be interested to know what now happens after #45258 which removes the need for DBAL for column renames. |
Beta Was this translation helpful? Give feedback.
-
It is now fixed on 10.x using native schema operations: #45487 $table->enum('difficulty', ['easy', 'hard'])->change();
$table->renameColumn('difficulty', 'hardness'); or simply on MySQL: $table->enum('difficulty', ['easy', 'hard'])->renameTo('hardness')->change(); |
Beta Was this translation helpful? Give feedback.
It is now fixed on 10.x using native schema operations: #45487
or simply on MySQL: