SQLSTATE[42P01]: Undefined table: 7

I'm newbiу in laravel 9. I just want create simple blog with the tables:users, stories,comments.I have next relationship

users->stories(One To Many, hasMany laravel) , 
stories->users(One To Many (Inverse) / Belongs To), 
users->comments(One To Many, hasMany laravel) , 
comments->users(One To Many (Inverse) / Belongs To),
stories->comments(One To Many, hasMany laravel) , 
comments->stories(One To Many (Inverse) / Belongs To).

class User

<?php
    
    namespace App\Models;
    
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Sanctum\HasApiTokens;
    
    use App\Models\Story;
    use App\Models\Comment;
    
    class User extends Authenticatable
    {
        use HasApiTokens, HasFactory, Notifiable;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array<int, string>
         */
        protected $fillable = [
            'name',
            'email',
            'password',
        ];
    
        /**
         * The attributes that should be hidden for serialization.
         *
         * @var array<int, string>
         */
        protected $hidden = [
            'password',
            'remember_token',
        ];
    
        /**
         * The attributes that should be cast.
         *
         * @var array<string, string>
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
        
        public function stories()
        {
          return $this->hasMany(Story::class);
        }
        
        public function comments()
        {
          return $this->hasMany(Comment::class);
        }
    }

class Story:

<?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    use App\Model\User;
    use App\Model\Comment;
    
    class Story extends Model
    {
        use HasFactory;
        
         protected $fillable = ['description', 'textStory'];
         
         public function user()
        {
          return $this->belongsTo(User::class);
        }
        
        public function comments()
        {
          return $this->hasMany(Comment::class);
        }
    }

class Comment:

<?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    use App\Models\Story;
    use App\Models\User;
    
    class Comment extends Model
    {
        use HasFactory;
        
         public function storyFunction()
        {
          return $this->belongsTo(Story::class);
        }
         public function userFunction()
        {
          return $this->belongsTo(User::class);
        }
        
    }

Stories migration:

<?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('stories', function (Blueprint $table) {
                $table->id();
                $table->string('description');
                $table->text('textStory');
                $table->foreignId('user_id')->constrained()->onDelete('cascade');
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('stories');
        }
    };

Comments migration:

<?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('comments', function (Blueprint $table) {
                $table->id();
                $table->text('textComment');
                //$table->foreignId('story_id')->constrained('stories')->onDelete('cascade');
                /*$table->foreign('story_id')
              ->references('id')->on('stories')
              ->onDelete('cascade');*/
                $table->timestamps();
            });
            
            
       Schema::table('comments', function(Blueprint $table) {
           $table->foreignId('user_id')->constrained()->onDelete('cascade');
       });
       
       Schema::table('comments', function(Blueprint $table) {
           $table->foreignId('story_id')->constrained()->onDelete('cascade');
       });
       
       
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('comments');
        }
    };

But when i run php artisan migrate or php artisan migrate:refresh i see next error:

SQLSTATE[42P01]: Undefined table: 7 ?z?????С?s??: ??NВ????N??╡?????╡ "stories " ???╡ N?N?NЙ?╡N?NВ??N??╡NВ (SQL: alter table "comments" add constraint "comment s_story_id_foreign" foreign key ("story_id") references "stories" ("id") on dele te cascade)

But if i delete or comment next strings in comments migration file:

Schema::table('comments', function(Blueprint $table) {
       $table->foreignId('story_id')->constrained()->onDelete('cascade');
   });

Migration run succesfull.

Please help me resolve this error.

Upvotes: 2

Views: 2015

Answers (1)

Liudmila Savateeva
Liudmila Savateeva

Reputation: 384

Most likely, the table that Laravel finds using the conventions is missing. Specify the table explicitly as an argument to the constrained method like this Schema::table('comments', function(Blueprint $table) { $table->foreignId('story_id')->constrained('Your_table_name')->onDelete('cascade'); });Foreign Key Constraints

Upvotes: 1

Related Questions