laravel 迁移语法错误或访问冲突“重复键名更改表并添加索引

laravel migration syntax error or access violation "Duplicate key name alter table and add index

提问人:etranz 提问时间:2/3/2023 更新时间:2/3/2023 访问量:210

问:

我正在开发一个新的代码库,它给了我用 laravel 编写的

我尝试运行迁移,但出现以下错误

SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'artisan_services_user_id_index' (SQL: alter table `artisan_services` add index `artisan_services_user_id_i
ndex`(`user_id`))

这是迁移代码。

 public function up()
    {
        Schema::create('artisan_services', function (Blueprint $table) {
            $table->id();
            $table->string('user_id',10)->nullable();
            $table->text('profile_picture')->nullable();
            $table->longText('title')->nullable();
            $table->longText('full_name')->nullable();
            $table->text('feature_image')->nullable();
            $table->text('business_category')->nullable();
            $table->string('phone',50)->nullable();
            $table->longText('email')->nullable();
            $table->longText('website')->nullable();
            $table->text('cost')->nullable();
            $table->longText('per_service')->nullable();
            $table->longText('street_address')->nullable();
            $table->longText('city')->nullable();
            $table->longText('state')->nullable();
            $table->longText('service_description')->nullable();
            $table->longText('experience')->nullable();
            $table->string('status',10)->default(0)->nullable();
            $table->longText('rating')->nullable();
            $table->longText('review')->nullable();
            $table->longText('employers')->nullable();
            $table->timestamps();

        });
    }

   
    public function down()
    {
        Schema::dropIfExists('artisan_services');
    }
PHP Laravel 迁移 语法错误

评论


答:

0赞 Milly Khamroev 2/3/2023 #1

说你已经以某种方式创建了该索引。您需要在创建之前删除该索引。将此代码放在顶部

 $table->dropIndex('artisan_services_user_id_index');