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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:32:11  来源:igfitidea点击:

Laravel Migrations - Dropping columns

phplaravelsymfonydoctrine-orm

提问by Black

I need to drop the column UserDomainNamefrom my database table clients.

我需要UserDomainName从我的数据库表中删除该列clients

At first I installed doctrine/dbalby executing composer require doctrine/dbalfollowed 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 migratebut no Column is dropped. If I execute it again I get Nothing to migrate.

执行后php artisan migrate但没有删除列。如果我再次执行它,我会得到Nothing to migrate.

回答by Jerodev

The downfunction is used for rollbacks, you have to add this dropColumnin the upfunction because it is an action you want to perform when running migrations.

down函数用于回滚,您必须dropColumnup函数中添加它,因为这是您在运行迁移时要执行的操作。

So, in your upfunction there should be:

所以,在你的up函数中应该有:

Schema::table('clients', function (Blueprint $table) {
    $table->dropColumn('UserDomainName');
});

And in the downfunction 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.

这样,您始终可以返回到迁移中的任何一点。