خطای تعریف foreign key در artisan لاراول

من هر وقت تو تیبل forign key تعریف میکنم بعد از این که php artisan migrate تیبل تو دیتابی

php artisan migrate
Migrating: 2019_04_04_073026_create_photos_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `digistyle`.`#sql-1080_89` (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table `photos` add constraint `photos_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

  at C:\xampp\htdocs\laravel\digistyle\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `digistyle`.`#sql-1080_89` (errno: 150 "Foreign key constraint is incorrectly formed"
)")
      C:\xampp\htdocs\laravel\digistyle\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\laravel\digistyle\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

س ساخته میشه بدون forign key و این ارور رو میده

برچسب ها:
پرسیده شده در: 5 سال قبل
آمار بازدید: 3441
0

برای حل این موضوع می بایست  فیلد user_id رو از نوع unsignedInteger  تعریف کنید و دوباره migrate  رو انجام بدید.

ارسال شده در: 5 سال قبل
omrani71 (40)
بیش از ۲ سال عضویت در روکسو بیش از ۲ سال عضویت در روکسو
0

مشکل با اضافه کردن :

 Schema::dropIfExists('photos');

به public function up در migration حل شد .

public function up()
    {
        Schema::dropIfExists('photos');
        Schema::create('photos', function (Blueprint $table) {
            $table->Increments('id');
            $table->string('path');
            $table->string('original_name');
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('photos');
    }

 

ارسال شده در: 5 سال قبل
1

دوستان توجه داشته باشند اگر از لاراول 5.8 استفاده میکنید، در این نسخه مقدار id بصورت bigIncrements تعریف شده است. بنابراین باید کلید خارجی را هم بصورت unsignedBigInteger بنویسید.

ارسال شده در: 5 سال قبل
saeid2020 (30)
بیش از ۱ سال عضویت در روکسو بیش از ۱ سال عضویت در روکسو
جهت ارسال پاسخ ابتدا عضو سایت شوید.