如何在不丢失数据的情况下更改数据类型并通过迁移添加列? (laravel 5.3)
How to change data type and add column with migration without losing data? (laravel 5.3)
我的迁移是这样的:
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
我想更改数据类型并在 table 测试中添加列。然后更新数据库中的table。我这样编辑:
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
然后我运行:php artisan migrate:refresh
我丢失了数据库中的数据
有没有人可以帮助我?
使用以下命令创建新的迁移文件:
php artisan make:migration name_of_your_file
它的功能应该是这样的
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
现在只需 运行 命令下方
php artisan migrate
注意:不要使用refresh
它会删除数据
要了解有关 Laravel 迁移的更多信息,请参阅:https://laravel.com/docs/5.3/migrations#modifying-columns
我的迁移是这样的:
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
我想更改数据类型并在 table 测试中添加列。然后更新数据库中的table。我这样编辑:
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
然后我运行:php artisan migrate:refresh
我丢失了数据库中的数据
有没有人可以帮助我?
使用以下命令创建新的迁移文件:
php artisan make:migration name_of_your_file
它的功能应该是这样的
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
现在只需 运行 命令下方
php artisan migrate
注意:不要使用refresh
它会删除数据
要了解有关 Laravel 迁移的更多信息,请参阅:https://laravel.com/docs/5.3/migrations#modifying-columns