user12923670
user12923670

Reputation:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")

I just deployed on Heroku yesterday and connected to a Postgresql db and since then I have this fun error showing up on my screen (and terminal) on Heroku:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")

In my terminal, below this error, I have an Undefined table error stating that my categories table doesn't exist. This so frustrating because it does exist and it's right there! Can someone help with this? Has anyone had a similar Issue?

Tried:

The migrations run up until the stories table where the relationship is located and then stop running. Right below stories is the categories table. I don't know how much this helps in fining a solution.

stories table:

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

categories table:

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

Story model:

        <?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);
        }

    }

Category model:

    <?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);
        }

    }

Been fixated on this for days, maybe you guys can see something I don't. Thank you so much in advance.

Upvotes: 0

Views: 19945

Answers (4)

reza sabzi nejad
reza sabzi nejad

Reputation: 59

you have to consider that first the Parent migration should be created and then the relationship should be established. Create the "categories" migration first Then create the "stories" migration. I hope it is useful..

Upvotes: 1

Sumit Kumar
Sumit Kumar

Reputation: 1902

You can create a single migration file in these scenarios.

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

Upvotes: 0

Hamed Yarandi
Hamed Yarandi

Reputation: 1211

You should change date in migration file names

Example

2019_07_29_113341_create_categories_table
2019_07_30_113341_create_stories_table

the older date time would be the first to run,

you should migrate categories table then stories

Hope help you

Upvotes: 3

Tohid Dadashnezhad
Tohid Dadashnezhad

Reputation: 1938

That's because your categories table migration runs after stories table however stories table depends on categories because of the foreign keys. What you have to do is :

  • Rename the categories table migration and move it to top of the stories table migration
  • Drop all tables in the the DB
  • Run your migration again

Upvotes: 1

Related Questions