gpt4 book ai didi

php - Laravel 迁移 - 删除列

转载 作者:可可西里 更新时间:2023-10-31 23:59:57 26 4
gpt4 key购买 nike

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

首先,我通过执行 composer require doctrine/dbal 然后是 composer update 安装了 doctrine/dbal,如 documentation 中所述.

然后我创建了我想用来删除列的迁移:

php artisan make:migration remove_user_domain_name_from_clients --table=clients

我将 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');
});
}
}

但是,我明白了

Migrating: 2017_08_22_135145_remove_user_domain_name_from_clients
Migrated: 2017_08_22_135145_remove_user_domain_name_from_clients

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

最佳答案

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

因此,在您的 up 函数中应该有:

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

而在 down 函数中,您应该执行相反的操作,将列添加回去:

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

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

关于php - Laravel 迁移 - 删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com