How about writing the reverse of it in down() function:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('display_name', 'name');
});
I'm trying to add a username column to my users table (which currently just has the standard columns provided in a new Laravel project on it). To avoid confusion with the name column, I wanted to first rename the name column to display_name.
I attempted to do so using the following migration:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'display_name');
});
}
Which results in the following exception:
SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed (SQL: DROP TABLE users)
I was immediately concerned that I coded something wrong, but upon further inspection, this appears to be the standard for SQLite. Here's the SQL the migration is trying to run:
CREATE TEMPORARY TABLE __temp__users AS
SELECT
id,
name,
email,
email_verified_at,
remember_token,
created_at
updated_at,
password
FROM users
DROP TABLE users
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
email VARCHAR(255) NOT NULL COLLATE BINARY,
email_verified_at DATETIME DEFAULT NULL,
remember_token VARCHAR(255) DEFAULT NULL COLLATE BINARY,
created_at DATETIME DEFAULT NULL,
updated_at DATETIME DEFAULT NULL,
password VARCHAR(60) DEFAULT NULL COLLATE BINARY,
display_name VARCHAR(255) NOT NULL
)
INSERT INTO users (
id,
display_name,
email,
email_verified_at,
remember_token,
created_at,
updated_at,
password
)
SELECT
id,
name,
email,
email_verified_at,
remember_token,
created_at,
updated_at,
password
FROM __temp__users
DROP TABLE __temp__users
This is basically creating a temp table with the new column, dropping the original table, then recreating the table, and filling in the data. While this would technically get the job done, this also ignores all issues regarding foreign keys.
I understand that there's probably ways around this (such as just creating a new column and dropping the original, or disabling foreign key constraints for migration), but I haven't seen any mentioning of this requirement on the Laravel documentation, and I'm currently under the impression that what I'm doing is supposed to work without any gimmicks.
Am I doing something incorrectly here, or is this a bug?
Please or to participate in this conversation.