【问题标题】:SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")SQLSTATE [42P01]:未定义表:7 错误:关系“类别”不存在第 1 行:从“类别”中选择 * ^(SQL:从“类别”中选择 *)
【发布时间】: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


    【解决方案1】:

    您必须考虑首先应创建父迁移,然后应建立关系。 首先创建“类别”迁移然后创建“故事”迁移。 希望有用。。

    【讨论】:

      【解决方案2】:

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

      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('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');
          }
      }
      

      【讨论】:

        【解决方案3】:

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

        例子

        2019_07_29_113341_create_categories_table
        2019_07_30_113341_create_stories_table
        

        较早的日期时间将首先运行,

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

        希望能帮到你

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-22
            • 2020-02-19
            • 1970-01-01
            • 2020-04-28
            • 1970-01-01
            相关资源
            最近更新 更多