Laravel迁移,检查hasColumn却得到“Column Exists”MySQL错误

4

我正在尝试创建一个迁移,向现有表添加一列。

up()方法用于创建该表时如下所示:

public function up()
    {
        Schema::create('lead_assignments', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('version');
            $table->integer('reactor_track_id')->unsigned();
            $table->integer('lead_id')->unsigned();
            $table->integer('agent_id')->unsigned();
            $table->integer('lead_stage_id')->unsigned();
            $table->integer('managed_by_id')->unsigned();
            $table->integer('side_of_deal_id')->unsigned();
            $table->integer('coordinator_id')->unsigned();
            $table->decimal('referral_fee', 15, 2);
            $table->timestamp('response_ts');
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('lead_id')->references('id')->on('leads');
            $table->foreign('agent_id')->references('id')->on('agents');
            $table->foreign('lead_stage_id')->references('id')->on('lead_stages');
            $table->foreign('managed_by_id')->references('id')->on('managed_bys');
            $table->foreign('side_of_deal_id')->references('id')->on('side_of_deal');
            $table->foreign('coordinator_id')->references('id')->on('users');
        });
    }

我的Alter up()方法看起来像这样:
   public function up()
    {
        Schema::table('lead_assignments', function (Blueprint $table) {
            if (!Schema::hasColumn('lead_assignments', 'property_type_id')) {
                $table->integer('property_type_id')->unsigned();

                $table->foreign('property_type_id')
                    ->references('id')
                    ->on('property_types');
            }
            if (!Schema::hasColumn('lead_assignments', 'referral_fee_expiration')) {
                $table->date('referral_fee_expiration')->nullable();
            }

           $table->dropForeign('lead_assignments_side_of_deal_id_foreign');
        });
        Schema::table('lead_assignments', function (Blueprint $table) {
            $table->integer('side_of_deal_id')->nullable()->change();
        });
    }

在这里,我试图添加property_types列,在创建之前先检查其是否存在,并在创建后将其设置为外键。 这个过程很顺利。 然后,我检查referral_fee_expiration列是否存在,如果不存在,则尝试创建它。
在运行“migrate install”之后进行初始迁移时,我遇到了MySQL错误:
[PDOException] SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'referral_fee_expiration'
对于此表,没有其他更改,我不确定为什么会抱怨该列已经存在,当除此之外没有其他迁移将其放置在那里,并且我正在检查它是否存在。
有任何建议吗?
注意:
这是从新的数据库开始。 我运行“php artisan migrate install”,然后运行“php artisan migrate”以产生错误。

1
这个问题可能很幼稚,但在调用up()函数前你是否回滚或删除了迁移? - Damien Pirsy
1个回答

0

如果您之前尝试运行迁移并且出现任何错误中断,则在这种情况下,导致错误的语句之前的语句已经被执行。但是,由于迁移在完全完成之前返回错误,因此它将显示状态为未迁移或待处理。

当您尝试重新运行迁移时,它将因重复列而给出错误,因为该语句在错误之前已执行,并且该列已添加到表中 - 这可能是原因。

在这种情况下,即使您回滚迁移,由于迁移本身未注册为“完成”状态,因此在抛出错误之前已执行的语句也不会回滚。

因此,您可能需要使用phpMyAdmin或类似工具检查MYSQL数据库,如果问题中的列存在于表中,请手动删除该列,然后再次尝试运行迁移。

希望它能帮助您解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接