【问题标题】:Laravel Migration, checking hasColumn but getting Column Exists MySQL ErrorLaravel 迁移,检查 hasColumn 但获取 Column Exists MySQL 错误
【发布时间】:2016-05-27 23:04:37
【问题描述】:

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

对表进行初始创建的 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列,先检查它是否存在,然后在创建后将其设为外键。这很好用。然后我检查是否有 refer_fee_expiration 列,如果没有,我尝试创建它。

在运行“迁移安装”后进行初始迁移时,我收到此 MySQL 错误:

[PDO异常]
SQLSTATE [42S21]:列已存在:1060 列名重复 'referral_fee_expiration'

此表没有其他更改,我不确定为什么它抱怨该列已经存在,除了这个之外没有迁移将它放在那里,我正在检查它是否存在在创建之前。

有什么建议吗?

注意:

这是从一个新的数据库开始的。 我运行“php artisan migrate install”然后“php artisan migrate”产生错误。

【问题讨论】:

  • 愚蠢的问题,但是您在再次调用 up() 之前是否回滚/放弃了迁移?

标签: php mysql migration laravel-5.1


【解决方案1】:

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

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

在这种情况下,即使您回滚迁移,在引发错误之前已经执行的语句也不会回滚,因为迁移本身没有注册状态完成。

因此您可能必须使用 phpMyAdmin 或任何此类工具检查 MYSQL 数据库,如果表中存在相关列,请手动删除该列并尝试再次运行迁移。

希望它可以帮助您解决问题。

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 2018-10-01
    • 2018-12-06
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2021-08-04
    • 2020-08-16
    相关资源
    最近更新 更多