راکت
حل شده

خطای migration

1 پاسخ

سلام من میحام جدول medai را migrate کنم این خطا را میده برای حل این خطا باید چیکار کنم
در ضمن جدول users اول migrate میشه بعد جدول media ایجاد میشه پس نباید از user_id توی مایگریشن media ایراد بگیره!!

جدول users

2014_10_12_000000_create_users_table.php

جدول media

2021_02_17_175116_create_media_table.php
H:\xampp\htdocs\lms0                                                                                                                                                         
λ php artisan migrate                                                                                                                                                        
Migrating: 2021_02_17_175116_create_media_table                                                                                                                              

   Illuminate\Database\QueryException                                                                                                                                        

  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'user_id' doesn't exist in table (SQL: alter table `users` add constraint `users_user_id_foreign` foreig
n key (`user_id`) references `users` (`id`) on delete SET NULL)                                                                                                              

  at H:\xampp\htdocs\lms0\vendor\laravel\framework\src\Illuminate\Database\Connection.php:678                                                                                
    674▕         // If an exception occurs when attempting to run a query, we'll format the error                                                                            
    675▕         // message to include the bindings with SQL, which will make this exception a                                                                               
    676▕         // lot more helpful to the developer instead of just the database's errors.                                                                                 
    677▕         catch (Exception $e) {                                                                                                                                      
  ➜ 678▕             throw new QueryException(                                                                                                                               
    679▕                 $query, $this->prepareBindings($bindings), $e                                                                                                       
    680▕             );                                                                                                                                                      
    681▕         }                                                                                                                                                           
    682▕                                                                                                                                                                     

  1   H:\xampp\htdocs\lms0\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471                                                                               
      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'user_id' doesn't exist in table")                                                  

  2   H:\xampp\htdocs\lms0\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471                                                                               
      PDOStatement::execute()                                                                                                                                                
    public function up()
    {
        Schema::create('media', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('files');
            $table->enum('type',\Lms\Media\Models\Media::$types);
            $table->string('filename');
            $table->boolean('is_private');
            $table->unsignedBigInteger('category_id')->nullable();
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL');
        });
    }
ثبت پرسش جدید

پاسخ‌ها

1
سینا خاقانی
سینا خاقانی
@sina.it91

تخصص: توسعه دهنده بک‌اند

بهترین پاسخ

@MajidDeveloper
سلام
کلید خارجی رو در جدول media به صورت زیر تعریف کنید

$table->foreignId('user_id')
$table->foreign('user_id')->references('id')->on('media')

شاید پاسخ شما همین‌جا باشد

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام