Errors

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

Ravindra Kumar

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();
        });

 

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

imagick php extension must be installed to use this driver in Laravel

It means that your Laravel app (or a package like Intervention Image, Spatie Media Library, or Laravel Snappy) is trying to use the Imagick image processing library, but it’s not installed or enabled on your server. If you’re using Ubuntu/Debian (Linux server): sudo apt-get update sudo apt-get install -y php-imagick sudo service apache2 restart # […]

Driver (intervention\image\drivers\gd\driver) could not be instantiated. laravel 10

The error: Driver (intervention\image\drivers\gd\driver) could not be instantiated. means that Intervention Image v3 (which you’re likely using with Laravel 10) cannot find or instantiate the GD driver. 1. Install GD or Imagick PHP extension Make sure you have GD or Imagick PHP extension installed and enabled. Check GD (on your server or local): php -m […]

Call to undefined method Intervention\Image\ImageManager::make() in Laravel

The error “Call to undefined method Intervention\Image\ImageManager::make()” typically happens when you’re trying to use the make() method from the Intervention Image package but haven’t correctly set up the manager or are using the wrong instance. Solution Make sure you’re using Intervention\Image\Facades\Image facade, not ImageManager directly. 1. Correct Usage in Laravel In your controller or wherever […]

Class ‘Intervention\Image\ImageServiceProvider’ not found in Laravel

The error “Class ‘Intervention\Image\ImageServiceProvider’ not found” in Laravel usually occurs when the Intervention Image package is not properly installed or configured. Steps to Fix: 1. Install Intervention Image Package Run the following command to install the package via Composer: composer require intervention/image 2. Check config/app.php (For Laravel <= 5.4) If you’re using Laravel 5.4 or […]

Illuminate \ Contracts \ Encryption \ DecryptException The payload is invalid in laravel

The error “Illuminate \ Contracts \ Encryption \ DecryptException: The payload is invalid” in Laravel occurs when the decryption of an encrypted value fails. This can happen due to several reasons: Possible Causes & Solutions: 1. Incorrect Encryption Key (APP_KEY) Laravel uses the encryption key defined in the .env file (APP_KEY). If the key is […]

SQLSTATE[HY001]: Memory allocation error: 1038 Out of sort memory, consider increasing server sort buffer size

The error SQLSTATE[HY001]: Memory allocation error: 1038 Out of sort memory, consider increasing server sort buffer size occurs when MySQL runs out of memory while sorting data. To resolve this issue, follow these steps: 1. Increase sort_buffer_size in MySQL Configuration The sort_buffer_size determines the amount of memory allocated for sorting operations. Increasing it can help […]