【发布时间】:2020-06-03 06:46:52
【问题描述】:
我昨天刚刚在 Heroku 上部署并连接到 Postgresql 数据库,从那时起,我在 Heroku 的屏幕(和终端)上出现了这个有趣的错误:
SQLSTATE[42P01]:未定义表:7 错误:关系“类别”不存在第 1 行:从“类别”中选择 * ^(SQL:从“类别”中选择 *)
在我的终端中,在此错误下方,我有一个未定义的表错误,指出我的类别表不存在。这太令人沮丧了,因为它确实存在并且就在那里!有人可以帮忙吗?有没有人有类似的问题?
试过了:
- 回滚表:heroku run php artisan migrate:rollback
- 新鲜迁移:heroku 运行 php artisan migrate:fresh
- 迁移重置:heroku 运行 php artisan 迁移:重置
迁移一直运行到关系所在的 stories 表,然后停止运行。故事的正下方是类别表。我不知道这对解决问题有多大帮助。
故事表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stories');
}
}
分类表:
<?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->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
故事模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Reviews;
use App\User;
use App\Category;
use App\ReadingList;
class Story extends Model
{
protected $guarded = [];
public function readingList()
{
return $this->belongsTo(ReadingList::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function reviews() {
return $this->hasMany(Reviews::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
类别型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Story;
class Category extends Model
{
protected $guarded = [];
public function story()
{
return $this->hasMany(Story::class);
}
}
这几天一直关注这个问题,也许你们能看到一些我看不到的东西。非常感谢你。
【问题讨论】:
标签: php laravel postgresql