-1

i have Category model, and i want to use it for posts and topics. and i think should use many to many ploymorphic relation, but in migrating i get this error:

  Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forum.categoriables' doesn't ex
ist (SQL: alter table `categoriables` add `category_id` int unsigned not null, add `categoriable_id` int unsigned not null, add `ca
tegoriable_type` varchar(191) not null)

this is categories table:

 Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();

            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->unsignedInteger('parent_id');
            $table->foreign('parent_id')->references('id')->on('topics')->onDelete('cascade');

            $table->timestamps();
        });

and this is categoriables table:

Schema::table('categoriables', function (Blueprint $table) {
            $table->unsignedInteger('category_id');
            $table->unsignedInteger('categoriable_id');
            $table->string('categoriable_type');
        });

each of them are in separate migration file.

mr stark
  • 35
  • 1
  • 10
  • 1
    Migrations go in order of the date stamp prepended to the file name, so make sure 'categoriables' is first, then maybe 'categoriables' should be Schema::create instead of Schema::table? – Tarek Adam Feb 24 '19 at 22:06
  • No worries. Now that I re-read it, maybe 'categoriables' should be second actually, but that would only matter if you used the fk relations. – Tarek Adam Feb 24 '19 at 22:19

1 Answers1

0

I'm not too sure if this is what you want, but currently you are "ALTERING" the categoriable. Maybe try changing to Schema::create ?

Schema::create('categoriables', function (Blueprint $table) {
        $table->unsignedInteger('category_id');
        $table->unsignedInteger('categoriable_id');
        $table->string('categoriable_type');
    });
Mees
  • 116
  • 3
  • 10