Laravel重命名列丢失所有数据 [英] Laravel rename column loss all data
问题描述
我是Laravel的新手,我使用php artisan migrate
命令创建了一个users表:
I am new to Laravel and I create a users table using php artisan migrate
command:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('username');
$table->string('email');
$table->string('password');
$table->rememberToken();
});
此后,我只需要将username
列更改为first_name
,然后按如下所示更改架构:
After that I just needed to change the username
column as first_name
then I change the schema as follows:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('first_name');
$table->string('email');
$table->string('password');
$table->rememberToken();
});
如果再次运行php artisan migrate
命令,它显示为Nothing to migrate
,则我使用了rollback
,并且丢失了所有表数据.如何在不影响数据的情况下编辑表结构?我讨厌Laravel文档
If I run the php artisan migrate
command again, it says Nothing to migrate
, then I used rollback
, and I lose all table data.. How can I edit table structure without affecting my data? I hate Laravel doc
推荐答案
让我们从您的模式开始.您的表名称是users
.它包含一个名为username
的列,您想将其更改为first_name
而不丢失现有数据.您需要为此更改创建一个新的迁移.
Let's start with your schema. Your table name is users
. It contains a column named username
and you want to change it to first_name
without losing existing data. You need to create a new migration for this change.
php artian make:migration rename_columns_to_users_table --table=users
将在您的迁移目录中创建一个新的迁移文件.打开它并像这样更改它:
A new migration file will be created in your migrations directory. Open it and change it like this:
Schema::table('users', function ($table) {
$table->renameColumn('username', 'first_name');
});
保存并再次运行
php artisan migrate
您的列名将立即重命名,而不会丢失您的旧数据.希望你现在明白了.
You column name will be renamed immediately without losing your old data. Hope you got it now.
您将在此处找到更多详细信息: https://laravel.com/docs/5.3 /migrations#renaming列
You will find more details here: https://laravel.com/docs/5.3/migrations#renaming-columns
这篇关于Laravel重命名列丢失所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!