سلام من توی جدولم رابطه ایجاد کردم و حالا این ارور رو دارم وقتی میخوام migrate کنم!
ک Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `requests_advers` add constraint `requests_advers_users_id_foreign` foreign key (`users_id`) references `users` (`user_id`)) د خود را اینجا وارد کنید
اینم جدولم
public function up() { Schema::create('requests_advers', function (Blueprint $table) { $table->bigIncrements('request_id'); $table->text('title'); $table->integer('type'); $table->integer('status')->default('0'); $table->unsignedBigInteger('province_id'); $table->foreign('province_id')->references('id')->on('provinces'); $table->unsignedBigInteger('city_id'); $table->foreign('city_id')->references('id')->on('cities'); $table->unsignedBigInteger('collection_id'); $table->foreign('collection_id')->references('id')->on('collections'); $table->unsignedBigInteger('category_id'); $table->foreign('category_id')->references('id')->on('categories'); $table->text('info'); $table->string('user_pic'); $table->unsignedBigInteger('users_id'); $table->foreign('users_id')->references('user_id')->on('users'); $table->unsignedBigInteger('ticket_id'); $table->foreign('ticket_id')->references('ticket_id')->on('tickets'); $table->timestamps(); }); }
جدول users
رو چک کنید ببینید id
چه نوع داده ایه و باید با user_id
توی این جدول یکی باشه.
یعنی اگه id
توی جدول users
به صورت زیر بود :
$table->increments('id');
توی جدول requests_advers
باید به این صورت باشه :
$table->unsignedInteger('users_id'); $table->foreign('users_id')->references('user_id')->on('users');
جدول users اینطوریه ببینید
public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('first_name',60); $table->string('last_name',60); $table->string('phone_num',11); $table->string('national_code',10)->nullable(); $table->string('gender')->nullable(); $table->string('birthday',10)->nullable(); $table->string('tell',11)->nullable(); $table->text('email')->nullable(); $table->integer('province')->nullable(); $table->integer('city')->nullable(); $table->text('address')->nullable(); $table->string('postal_code',10)->nullable(); $table->text('user_pic')->nullable(); $table->string('financial_situation',10)->nullable(); $table->string('user_status')->nullable(); $table->timestamps(); }); }
@pouriarahimi
توی متد references
جای user_id
، id
رو بذارین.
قبل مایگریشن چک کنید اگه جدول requests_advers
ساخته شده، Dropش کنید.
کدتون میشه این :
$table->foreign('users_id')->references('id')->on('users');
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟