byt*_*ker 40 laravel laravel-5.2
我有一个博客,其articles
表格Schema
定义如下:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('thumb')->nullable();
$table->text('excerpt');
$table->text('body');
$table->string('slug')->unique();
$table->integer('comment_count')->unsigned()->default(0);
$table->integer('view_count')->unsigned()->default(0);
$table->timestamps();
$table->softDeletes();
}
public function down()
{
Schema::drop('articles');
}
Run Code Online (Sandbox Code Playgroud)
我想删除列comment_count
,view_count
而不会丢失表中的现有数据
我定义了一个像这样的新迁移:
class RemoveCommentViewCount extends Migration
{
public function up()
{
//nothing here
}
public function down()
{
Schema::table('articles', function($table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');
});
}
}
Run Code Online (Sandbox Code Playgroud)
而且我做到了php artisan migrate
.它确实成功迁移,但两列并未删除.
我究竟做错了什么?如何在不丢失表中现有数据的情况下删除这些列?
San*_*r82 80
您的迁移必须如下所示:
Class RemoveCommentViewCount extends Migration
{
public function up()
{
Schema::table('articles', function($table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');
});
}
public function down()
{
Schema::table('articles', function($table) {
$table->integer('comment_count');
$table->integer('view_count');
});
}
}
Run Code Online (Sandbox Code Playgroud)
up方法中的dropColumn,因为要使用新的迁移删除此列.如果进行回滚,则还有两列时间
Cha*_*ari 12
即使您可以通过将数组列传递给 dropColumn函数,将多列放在一行中。
Class RemoveCommentViewCount extends Migration
{
public function up()
{
Schema::table('articles', function($table) {
$table->dropColumn(['comment_count', 'view_count']);
});
}
public function down()
{
Schema::table('articles', function($table) {
$table->integer('comment_count');
$table->integer('view_count');
});
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有外键约束,则首先删除外键索引关联,然后可以将列传递给 dropColumn 函数,以如下方式与其他人一起使用
public function up()
{
Schema::table('customer_orders', function($table) {
$table->dropForeign(['product_id']);
$table->dropForeign(['shipping_address_id']);
$table->dropColumn(['product_id', 'shipping_address_id', 'column1', 'column2']);
});
}
Run Code Online (Sandbox Code Playgroud)
创建删除列迁移
php artisan make:migration RemoveCommentViewCount
Run Code Online (Sandbox Code Playgroud)
该down
方法用于回滚,因此在up()
函数中添加 dropColumn 并在down()
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RemoveCommentViewCount extends Migration
{
public function up()
{
if (Schema::hasColumn('comment_count', 'view_count')){
Schema::table('articles', function (Blueprint $table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');
//OR
$table->dropColumn(['comment_count', 'view_count']);
});
}
}
public function down()
{
Schema::table('articles', function($table) {
$table->integer('comment_count');
$table->integer('view_count');
});
}
}
Run Code Online (Sandbox Code Playgroud)
检查有关删除列迁移的laravel 文档
归档时间: |
|
查看次数: |
49373 次 |
最近记录: |