質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

Q&A

1回答

11655閲覧

laravel SQLSTATE[42S21] 外部キー設定 エラー

yuki911

総合スコア27

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

0グッド

0クリップ

投稿2020/06/18 15:48

phpMyAdminを使ってlaravelで外部キー制約をつけようと指定いるのですが、
SQLSTATE [42S21]:列はすでに存在します:1060列名 'review_id'が重複しています。
と出てきています。
php artisan migrateをしたり、php artisan migrate:refreshをするとSQLSTATE [42S21]のエラーが出てきます。
データベースを消したりしても出てきます。
review_idに外部キーを設定したいのですがどうしたらいいのでしょうか?

エラー内容

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'review_id' (SQL: create table comments (id bigint unsigned not null auto_increment primary key, comment text not null, review_id bigint unsigned not null, created_at timestamp null, updated_at timestamp null, review_id bigint unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

laravel

1<?php 2//2020_06_17_233958_create_comments_table.php 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateCommentsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('comments', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->text('comment'); 19 $table->unsignedBigInteger('review_id'); 20 $table->timestamps(); 21 22 $table->foreignId('review_id')//外部キー、reviewが消えたらコメントも消える 23 ->constrained() 24 ->onDelete('cascade'); 25 }); 26 } 27 28 /** 29 * Reverse the migrations. 30 * 31 * @return void 32 */ 33 public function down() 34 { 35 Schema::dropIfExists('comments'); 36 } 37}

laravel

1<?php 2//2020_06_17_233959_create_reviews_table.php 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateReviewsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('reviews', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->string('title'); 19 $table->timestamps(); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('reviews'); 31 } 32} 33

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

エラーで出力されていたSQLは下記になります。

sql

1create table comments( 2 id bigint unsigned not null auto_increment primary key, 3 comment text not null, 4 review_id bigint unsigned not null, 5 created_at timestamp null, 6 updated_at timestamp null, 7 review_id bigint unsigned not null 8) default character 9set 10 utf8mb4 collate 'utf8mb4_unicode_ci' 11 )

CreateCommentsTableクラスのマイグレーションファイルでreview_idというカラムを2回定義されています。

PHP

1 public function up() 2 { 3 Schema::create('comments', function (Blueprint $table) { 4 $table->bigIncrements('id'); 5 $table->text('comment'); 6 $table->unsignedBigInteger('review_id');//1つ目:カラム生成+外部キー設定 7 $table->timestamps(); 8 9 $table->foreignId('review_id')//2つ目:カラム生成+外部キー設定 10 ->constrained() 11 ->onDelete('cascade'); 12 }); 13 } 14

投稿2020/06/18 17:26

m.kosuda

総合スコア153

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuki911

2020/06/19 04:55

カラムの生成を二度していたのですね! ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問