[英]How to add column in a table using laravel 5 migration without losing its data?
[英]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');
}
我想更改數據類型並在表測試中添加列。 然后更新數據庫中的表。 我這樣編輯:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.