mercredi 5 septembre 2018

Laravel php artisan migrate Have Error 150

i want to create category for company but i have this error : SQLSTATE[HY000]: General error: 1005 Can't create table sanat.#sql-53e_e7d (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table company_category add constraint company_category_company_id_foreign foreign key (company_id) references companies (id) on delete cascade)

this is my migration code for category:

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->string('name');
        $table->string('slug');
        $table->string('description')->nullable();
        $table->string('type');
        $table->string('imageUrl')->nullable();
        $table->timestamps();
    });
    Schema::create('article_category', function (Blueprint $table) {
        $table->integer('article_id')->unsigned();
        $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->primary(['article_id', 'category_id']);
    });
    Schema::create('company_category', function (Blueprint $table) {
        $table->integer('company_id')->unsigned();
        $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->primary(['company_id', 'category_id']);
    });

}

so company table and categories table dont create. when i remove this:

Schema::create('company_category', function (Blueprint $table) {
    $table->integer('company_id')->unsigned();
    $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
    $table->integer('category_id')->unsigned();
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
    $table->primary(['company_id', 'category_id']);
});

php artisan migrate worked . where is the problem

this is my company migration:

Schema::create('companies', function (Blueprint $table) {
    $table->increments('id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->boolean('active')->default(false);
    $table->string('name');
    $table->string('email')->unique();
    $table->string('slug');
    $table->text('aboutUs');
    $table->integer('tell');
    $table->string('address');
    $table->string('tag');
    $table->string('logoUrl');
    $table->string('slideUrl')->unique();
    $table->string('facebookUrl');
    $table->string('instagramUrl');
    $table->string('telegramUrl');
    $table->string('whatsAppUrl');
    $table->string('websiteUrl');
    $table->timestamps();
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire