我正在尝试创建一个迁移,向现有表添加一列。
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”以产生错误。