mohammed_alama
3 months ago
185
8
Laravel

SQL cannot add forign key

Posted 3 months ago by mohammed_alama

Hi i have error when hit artisan migrate:fresh to add forign key of user in projects migration

it show me that error

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `projects` add constraint `projects_owner_id_foreign` foreign key (`owner_id`) references `users` (`id`) on delete cascade)

and this is projects migration

  public function up()
    { 
        Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('owner_id')->unsigned();
            $table->string('title');
            $table->text('description');
            $table->timestamps();
       });
        Schema::table('projects', function($table) {
              $table->foreign('owner_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
        });
    }

Please sign in or create an account to participate in this conversation.