【问题标题】:Cannot remigrate migration after migration rollback Laravel 5.2迁移回滚 Laravel 5.2 后无法重新迁移迁移
【发布时间】:2018-08-01 22:27:52
【问题描述】:

美好的一天,我是 laravel 的新手,我正在执行迁移回滚,并且已成功完成

回滚:2018_02_22_172102_adding_fk_constrains_products_to_product_types_and_service_sub_types_table

但是当我尝试重新迁移时,我在下面遇到了这个错误。顺便说一句,我不想​​删除该列,因为它已经存在并且我不想丢失该列中的现有数据。我只想在这些表之间添加约束

[照亮\数据库\查询异常] SQLSTATE[23000]:违反完整性约束:1022 无法写入;表'#sql-2fc8_17c'中的重复键(SQL:更改表products添加约束products_product_type_id_foreign 更新级联时外键 (product_type_id) 引用 product_types (id)

[PDO异常] SQLSTATE[23000]:违反完整性约束:1022 无法写入;表'#sql-2fc8_17c'中的重复键

Table     Non_unique  Key_name                            Seq_in_index  Column_name
--------  ----------  ----------------------------------  ------------  -------------------
products           0  PRIMARY                                        1  id             
products           1  products_product_type_id_index                 1  product_type_id  
products           1  products_service_sub_type_id_index             1  service_sub_type_id

这是我的迁移代码

public function up()
{

    Schema::table('products',function (Blueprint $table){
        $table->integer('product_type_id')->unsigned()->index()->change();
        $table->foreign('product_type_id')->references('id')->on('product_types')->onUpdate('cascade');

        $table->integer('service_sub_type_id')->nullable()->unsigned()->index()->change();
        $table->foreign('service_sub_type_id')->references('id')->on('service_sub_types')->onUpdate('cascade');

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{

    Schema::table('products', function(Blueprint $table){
        $table->dropForeign(['product_type_id']);
        $table->dropForeign(['service_sub_type_id']);
    });

}

【问题讨论】:

  • 您可能需要检查您的 MySQL 数据库以查看外键是否确实存在,否则您可以尝试使用 Schema::disableForeignKeyConstraints();
  • Schema::disableForeignKeyConstraints();我在 down() 方法中添加了这段代码,但错误仍然存​​在。
  • 目前您的数据库中是否确实存在该键/列?
  • 是的。它存在于我的数据库中。我在更新后的问题 SHOW INDEXES FROM products 中添加了这个查询的结果;
  • 你看到的问题是键名实际上是'products_product_type_id_index',删除外键的laravel命名约定是使用_foreign后缀,所以在你的情况下它应该是product_type_id_foreign

标签: migration laravel-5.2


【解决方案1】:

我只是在迁移中删除了以下代码

$table->integer('product_type_id')->unsigned()->index()->change();

$table->integer('service_sub_type_id')->nullable()->unsigned()->index()->change();

public function up()
{

   Schema::table('products',function (Blueprint $table){
      //$table->integer('product_type_id')->unsigned()->index()->change();
      $table->foreign('product_type_id')->references('id')->on('product_types')->onUpdate('cascade');

      //$table->integer('service_sub_type_id')->nullable()->unsigned()->index()->change();
      $table->foreign('service_sub_type_id')->references('id')->on('service_sub_types')->onUpdate('cascade');

});
}

当我重新迁移时,它已成功迁移..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 2018-09-14
    • 2016-01-05
    • 2016-06-06
    • 2021-02-28
    • 1970-01-01
    • 2015-10-02
    • 2017-03-08
    相关资源
    最近更新 更多