Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Error Laravel 8 #7

Open
AimanHakeem opened this issue Jun 25, 2022 · 6 comments
Open

Migrate Error Laravel 8 #7

AimanHakeem opened this issue Jun 25, 2022 · 6 comments

Comments

@AimanHakeem
Copy link

I get this error when try to migrate the files to DB.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table services add constraint services_category_id_foreign foreign key (category_id) references categories (id) on delete cascade)

Running on Laravel 8 , MySQL 5.7 , PHP 8.1.7
Screenshot 2022-06-25 at 6 55 15 PM

@AimanHakeem
Copy link
Author

Yeah even using the installer still got migration issue.

Screenshot 2022-06-25 at 9 29 19 PM

Screenshot 2022-06-25 at 9 29 54 PM

really need help this part really confusing to solve this part, I did try find a solution around.

@ikramulmustafa
Copy link

ikramulmustafa commented Jul 1, 2022

Can you share the scheme of service table and categories table @AimanHakeem

Because sometimes its the difference between the type of column in tables

@AimanHakeem
Copy link
Author

@ikramulmustafa
service_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateServicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->id();
            $table->integer('category_id');
            $table->integer('api_provider_id')->nullable();
            $table->integer('api_provider_service_id')->nullable();
            $table->enum('type',['normal','api'])->default('normal');
            $table->enum('status',['active','deactive']);
            $table->text('name');
            $table->decimal('rate', 10, 4);
            $table->decimal('rate_original', 10, 4)->nullable();
            $table->integer('min');
            $table->integer('max');
            $table->float('percentage_increase')->nullable();;
            $table->text('description')->nullable();
            $table->timestamps();

           $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
           $table->foreign('api_provider_id')->references('id')->on('api_providers')->onDelete('cascade');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('services');
    }
}

categories_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->integer('sort')->nullable()->default(0);
            $table->enum('status',['active','deactive']);
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('categories');
    }
}

@ikramulmustafa
Copy link

Yeah even using the installer still got migration issue.

Screenshot 2022-06-25 at 9 29 19 PM Screenshot 2022-06-25 at 9 29 54 PM

According to the Image Your Category table is after the service but according to the scenario it should be reverse

Can You rename the Category table timestamp less then services table migration it will fixed your problem probably because your migration code seem fine

@ikramulmustafa
Copy link

2021-06-15_103121_create_categories_table

rename categories migration file to this

@AimanHakeem
Copy link
Author

Same issue , i tried to migrate the categories table first. @ikramulmustafa

Screenshot 2022-07-07 at 11 54 54 AM

Screenshot 2022-07-07 at 11 54 00 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants