Laravel更换列不起作用

时间:2017-11-09 10:20:02

标签: php postgresql laravel migration

Laravel: 5.4

数据库驱动程序: Postgres

我有多个Laravel架构,我想在其上迁移一些迁移。我的一次迁移如下:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class EditColumnPaymentMethodLengthOnRecurringPaymentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('recurring_payments', function (Blueprint $table) {
            $table->string('payment_method', 150)->nullable()->change();
            $table->text('test')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('recurring_payments', function (Blueprint $table) {
            $table->string('payment_method', 18)->nullable()->change();
            $table->dropColumn('test');
        });
    }
}

新的列被添加到数据库中(如果是rollBack则被删除),但“payment_method”列的长度始终为18个字符。除了第一个Schema,这个工作正常。

这是我用以下命令迁移的命令:

<?php

namespace App\Console\Commands;

use App\Account;
use App\Support\Schema;
use Illuminate\Console\Command;

class MigrateTenantsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tenant:migrate {--force : Force the operation to run when in production.}
                {--pretend : Dump the SQL queries that would be run.}
                {--seed : Indicates if the seed task should be re-run.}
                {--step : Force the migrations to be run so they can be rolled back individually.}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Migrate all tenants';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $tenants = Account::all();
        foreach($tenants as $tenant)
        {
            $schema = new Schema;

            // Migrate into the new schema
            $schema->migrate(
                $tenant->schema,
                $this->getOptionsFromArgs()
            );
        }
    }

    private function getOptionsFromArgs()
    {
        $options = [
            '--path' => 'database/migrations/tenants/schema'
        ];

        if($this->option('force'))
        {
            $options['--force'] = true;
        }
        if($this->option('pretend'))
        {
            $options['--pretend'] = true;
        }
        if($this->option('seed'))
        {
            $options['--seed'] = true;
        }
        if($this->option('step'))
        {
            $options['--step'] = true;
        }
        return $options;
    }
}

@ jedrzej.kurylo指出用--pretend运行命令。当我这样做时,我得到了一个例外:[Doctrine\DBAL\Schema\SchemaException] There is no column with name 'payment_method' on table 'recurring_payments'.

桌子在那里不是问题。这可能是Doctrine没有找到正确的Schema配置的问题,因此使用公共Schema配置。在公共模式中,表不存在但我不想使用公共模式。我在这里做错了,我怎么能告诉Doctrine使用正确的Schema配置?那么Doc​​trine第一次找到合适的配置至少是看起来像是什么样的。

原则的架构/架构类:

public function __construct(
        array $tables = array(),
        array $sequences = array(),
        SchemaConfig $schemaConfig = null,
        array $namespaces = array()
    ) {
        if ($schemaConfig == null) {
            $schemaConfig = new SchemaConfig();
        }
        $this->_schemaConfig = $schemaConfig;

        $this->_setName($schemaConfig->getName() ?: 'public');

        foreach ($namespaces as $namespace) {
            $this->createNamespace($namespace);
        }

        foreach ($tables as $table) {
            $this->_addTable($table);
        }

        foreach ($sequences as $sequence) {
            $this->_addSequence($sequence);
        }
    }

0 个答案:

没有答案