LoginSignup
dddddddddddd
@dddddddddddd (陸 加治屋)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

php artisan migrateの実行で外部制約によるエラーが出た

解決したいこと

laravel9にてマイグレーションで"php artisan migrate"を実行すると以下のようなエラーが出ました

 SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'products' already exists (SQL: create table `products` (`id` bigint unsigned not null auto_increment primary key, `company_id` bigint unsigned not null, `product_name` varchar(255) not null, `price` int not null, `stock` int not null, `comment` varchar(255) null, `img_path` varchar(255) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

データベースに既にテーブルが存在するがためのエラーみたいなので"php artisan migrate:fresh"を実行してテーブルを消そうとすると以下のようなエラーが出ました

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `products` add constraint `products_company_id_foreign` foreign key (`company_id`) references `companies` (`id`) on delete cascade)

外部制約関連のことが引っ掛かったみたいで"FAIL"のでたマイグレーションファイルを見直し訂正しました 
ですがまだこの有様です もう初心者の自分にはわからないのでご教授をお願いします
問題のファイル

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('companie_id')->unsigned();
            $table->string('product_name');
            $table->integer('price');
            $table->integer('stock');
            $table->string('comment')->nullable();
            $table->string('img_path');
            $table->timestamps();

            $table->foreign('companie_id')->references('id')->on('companies')->onDelete('cascade'); 
        });
    }

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

参照先ファイル

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('company_name');
            $table->integer('street_address');
            $table->char('representative_name',25);
            $table->timestamps();
        });
    }

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

Windows10
laravel9
php8.1
MAMP

0

2Answer

Comments

  1. @dddddddddddd

    Questioner

    無理です 外部制約が邪魔をしてきます

  2. @dddddddddddd

    Questioner

    2023_06_04_145946_create_products_table ........................... 6ms FAIL
    すっとこれ

"php artisan migrate:fresh"を実行してテーブルを消そうとすると以下のようなエラーが出ました

"Cannot add foreign key constraint"は外部キー追加時のエラーなので、テーブルは削除できていて、作成するときにエラーが出ているのではないでしょうか。

productsテーブルからcompaniesテーブルを参照するようになっていますが、companiesテーブルが先に作成されるようになっていますか?

0

Comments

  1. @dddddddddddd

    Questioner

    なんとか自力で解決しました 長かったです

Your answer might help someone💌