Ravindra24.com

(“SQLSTATE[HY000]: General error: 1005 Can’t create table (errno: 121 “Duplicate key on write or update”)”)

I am getting the error (“SQLSTATE[HY000]: General error: 1005 Can’t create table (errno: 121 “Duplicate key on write or update”)”) on my Laravel project i have three migrations which are following

users migration

Schema::create('users', function (Blueprint $table) {
            //$table->increments('id');
            $table->string('name');
            $table->string('email')->primary();
            $table->string('mobile');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('role');
            $table->string('plan');
            $table->string('job_portal_role')->nullable();
            $table->boolean('status');
            $table->rememberToken();
            $table->timestamps();
            
        });

business_profiles migration


Schema::create('business_profiles', function (Blueprint $table) {
            $table->id();
            $table->string('user_id');
            $table->foreign('user_id',70)->references('email')->on('users');
            $table->string('name',50);
            $table->string('mobile',50);
            $table->string('business_name',100);
            $table->string('business_email',100);
            $table->string('business_phone',100);
            $table->text('business_description');
            $table->string('business_category',100);
            $table->string('country',100);
            $table->string('state',100);
            $table->string('city',100);
            $table->string('website',100);
            $table->string('facebook',100);
            $table->string('whatsapp',100);
            $table->string('instagram',100);
            $table->string('youtube',100);
            $table->boolean('status');
            
            $table->timestamps();
        });

ads migration

Schema::create('ads', function (Blueprint $table) {
            $table->id('ad_id');
            $table->string('ad_user_id');
            $table->foreign('ad_user_id',100)->references('email')->on('users');
            $table->string('title');
            $table->string('ad_details');
            $table->string('picture1');
            $table->string('picture2');
            $table->string('picture3');
            $table->string('picture4');
            $table->string('picture5');
            $table->string('category');
            $table->string('email');
            $table->string('mobile');
            $table->string('website');
            $table->boolean('status');
            $table->string('ad_type');
            $table->timestamps();
        });

When I try to run migration command on terminal of my visual studio code it shows me following error.
(“SQLSTATE[HY000]: General error: 1005 Can’t create table (errno: 121 “Duplicate key on write or update”)”)

 

Solution

You are using wrong syntax in ads migration file so following is the correct syntax and when you are making reference you should not define the character limit you can specify this limit into your column.

Schema::create('ads', function (Blueprint $table) {
            $table->id('ad_id');
            $table->string('ad_user_id',100);
            $table->foreign('ad_user_id')->references('email')->on('users');
            $table->string('title');
            $table->string('ad_details');
            $table->string('picture1');
            $table->string('picture2');
            $table->string('picture3');
            $table->string('picture4');
            $table->string('picture5');
            $table->string('category');
            $table->string('email');
            $table->string('mobile');
            $table->string('website');
            $table->boolean('status');
            $table->string('ad_type');
            $table->timestamps();
        });

 

Leave a Comment