前提・実現したいこと
laravelを使って簡単な掲示板のようなアプリを作ろうとしています。本番環境はherokuを使っています。
ローカル環境では問題なく"ポスト"を投稿できるのですが、本番環境だとカラムが無いといったニュアンスのエラーが出ます。自分なりに調べているとdbの違いがエラーを生んでいるのかとも思っています。
ですが、"ポスト"の投稿ボタンを押す前にherokuのログをみてみると、既にstatus=500のエラーが出ておりその点も気になります。
色々、エラーの可能性を感じる点はあるのですがこれといった確証が無く、今回質問させてお頂きました。当方、プログラミングを初めてまだ3ヶ月の若輩者なのですが何卒、お力を貸していただければと思います。宜しく御願い致します。
発生している問題・エラーメッセージ
ローカル環境では投稿ボタンを押した際には無事に投稿できるが、本番環境で同じことをすると以下のエラーがでます。"categories"に(category_id)が無い的な事かと、、
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "posts" violates foreign key constraint "posts_category_id_foreign"
DETAIL: Key (category_id)=(1) is not present in table "categories". (SQL: insert into "posts" ("user_id", "category_id", "content", "title", "updated_at", "created_at") values (1, 1, lll, kkl, 2019-05-28 06:06:27, 2019-05-28 06:06:27) returning "id")
該当のソースコード
使用言語 php
postテーブル作成時のマイグレーションファイル
<?php
```ここに言語を入力
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->bigInteger('category_id')->unsigned()->index();
$table->string('title')->nullable();
$table->text('content')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
categoriesテーブル作成時のマイグレーションファイル
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category_name')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
```
### 試したこと
rake db:resetで一回リセットし直した後、heroku run php artisan migrate は行いましたが、結果は同じでした。
### 補足情報(FW/ツールのバージョンなど)
開発環境
ローカル
musql: ver 5.6.42,
laravel: ver 5.8.17
php: ver 7.2.18
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/28 07:07
2019/05/28 07:26 編集
2019/05/28 11:08
2019/05/28 11:12
2019/05/28 11:13
2019/05/28 11:13
2019/05/28 11:16