Laravel 迁移 - 删除列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45819718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Migrations - Dropping columns
提问by Black
I need to drop the column UserDomainName
from my database table clients
.
我需要UserDomainName
从我的数据库表中删除该列clients
。
At first I installed doctrine/dbal
by executing composer require doctrine/dbal
followed by composer update
, like described in the documentation.
起初我doctrine/dbal
通过执行composer require doctrine/dbal
然后执行安装composer update
,就像文档中描述的那样。
Then I created the migration which I want to use to drop the column:
然后我创建了我想用来删除列的迁移:
php artisan make:migration remove_user_domain_name_from_clients --table=clients
I added Schema::dropColumn('UserDomainName');
to the down()
method:
我添加Schema::dropColumn('UserDomainName');
到down()
方法中:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RemoveDomainName extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('clients', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('clients', function (Blueprint $table) {
Schema::dropColumn('UserDomainName');
});
}
}
However, I get
但是,我得到
Migrating: 2017_08_22_135145_remove_user_domain_name_from_clients
Migrated: 2017_08_22_135145_remove_user_domain_name_from_clients
after executing php artisan migrate
but no Column is dropped.
If I execute it again I get Nothing to migrate.
执行后php artisan migrate
但没有删除列。如果我再次执行它,我会得到Nothing to migrate.
回答by Jerodev
The down
function is used for rollbacks, you have to add this dropColumn
in the up
function because it is an action you want to perform when running migrations.
该down
函数用于回滚,您必须dropColumn
在up
函数中添加它,因为这是您在运行迁移时要执行的操作。
So, in your up
function there should be:
所以,在你的up
函数中应该有:
Schema::table('clients', function (Blueprint $table) {
$table->dropColumn('UserDomainName');
});
And in the down
function you should do the inverse, add the column back:
在down
你应该做的相反的函数中,将列添加回来:
Schema::table('clients', function (Blueprint $table) {
$table->string('UserDomainName');
});
This way, you can always return to any point in the migrations.
这样,您始终可以返回到迁移中的任何一点。