SQLSTATE[42P01]:未定义的表:7 错误:关系"categories"不存在 第 1 行:从 "categories" ^ 中选择 * (SQL:从"categories"中选择 * )



我昨天刚刚部署在Heroku上,并连接到Postgresql数据库,从那以后,我在Heroku:上的屏幕(和终端(上出现了这个有趣的错误

SQLSTATE[42P01]:未定义的表:7错误:关系"类别"不存在行1:从"类别"中选择*^(SQL:从"类别(

在我的终端中,在这个错误下面,我有一个未定义的表错误,说明我的类别表不存在。这太令人沮丧了,因为它确实存在,而且就在那里!有人能帮忙吗?有人有类似的问题吗?

尝试:

  • 回滚表:heroku运行php手工迁移:回滚
  • migrate fresh:heroku运行php-artisan migrate:fresh
  • 迁移重置:heroku运行php-artisan迁移:重置

迁移一直运行到关系所在的stories表,然后停止运行。故事的正下方是类别表。我不知道这对澄清溶液有多大帮助。

故事表:

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
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 IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
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 IlluminateDatabaseEloquentModel;
use AppReviews;
use AppUser;
use AppCategory;
use AppReadingList;
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 IlluminateDatabaseEloquentModel;
use AppStory;
class Category extends Model
{
protected $guarded = [];
public function story() 
{
return $this->hasMany(Story::class);
}
}

几天来我一直在关注这个,也许你们能看到一些我看不到的东西。事先非常感谢。

您应该更改迁移文件名中的日期

示例

2019_07_29_113341_create_categories_table
2019_07_30_113341_create_stories_table

较早的日期时间将是第一个运行的,

你应该迁移类别表然后故事

希望能帮助你

这是因为类别表迁移在故事表之后运行,但故事表由于外键而依赖于类别。你要做的是:

  • 重命名类别表迁移并将其移动到故事表迁移的顶部
  • 删除数据库中的所有表
  • 再次运行迁移

您必须考虑首先创建Parent迁移,然后建立关系。创建";类别";先迁移然后创建";故事";迁移我希望它有用。。

您可以在这些场景中创建一个迁移文件。

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateStoriesTable 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();
});
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('categories');
Schema::dropIfExists('stories');
}
}

最新更新