Good day, I'm new to laravel I was doing a migration rollback and it was successfully done
Rolled back: 2018_02_22_172102_adding_fk_constrains_products_to_product_types_and_service_sub_types_table
But when I try to re-migrate I encountered this error below. BTW I don't want to drop the column because it already existed and I don't want to lose the existing data in that column. I only want to add constraint between those tables
[Illuminate\Database\QueryException] SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-2fc8_17c' (SQL: alter table
productsadd constraintproducts_product_type_id_foreignforeign key (product_type_id) referencesproduct_types(id) on update cascade)[PDOException] SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#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
this my migration code
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']);
});
}
products; - Novice