I am trying to add unique
to slug
in products table and I have the following migration. However when I run php artisan migrate
I get an error.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddUniqueToProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->unique('slug');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropUnique('products_slug_unique');
});
}
}
Error
➜ php artisan migrate
[Illuminate\Database\QueryException] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'products_slug_unique' (SQL: alter table
products
add uniqueproducts_slug_unique
(slug
))[Doctrine\DBAL\Driver\PDOException] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'products_slug_unique'
[PDOException] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'products_slug_unique'
What am I doing wrong here?
slug
column is unique. – Amit Guptaproducts
addslug
varchar(255) not null) ... – shinupdate table_name set slug = NULL;
. Then run your unique migration. – Amit Gupta