Home > Software engineering >  Laravel 8 General error: 1215 Cannot add foreign key constraint
Laravel 8 General error: 1215 Cannot add foreign key constraint

Time:06-07

I get this error when i want to create teble from artisan tool.

Migrating: 2022_06_06_114027_create_subjekat_adrese_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `subjekat_adrese` add constraint `subjekat_adrese_subjekti_id_foreign` foreign key (`subjekti_id`) references `subjekti` (`id`) on delete cascade)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
    708▕         // If an exception occurs when attempting to run a query, we'll format the error
    709▕         // message to include the bindings with SQL, which will make this exception a
    710▕         // lot more helpful to the developer instead of just the database's errors.
    711▕         catch (Exception $e) {
  ➜ 712▕             throw new QueryException(
    713▕                 $query, $this->prepareBindings($bindings), $e
    714▕             );
    715▕         }
    716▕     }

       9 vendor frames 
  10  database/migrations/2022_06_06_114027_create_subjekat_adrese_table.php:37
      Illuminate\Support\Facades\Facade::__callStatic("create")

       22 vendor frames 
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

What i try:

   public function up()
    {
        Schema::create('subjekat_adrese', function (Blueprint $table) {
           
            $table->bigIncrements('id');
           
            $table->string('naziv');
            $table->string('adresa');

            $table->boolean('primarna')->unique();

            $table->bigInteger('subjekti_id')->index(); 
            $table->bigInteger('drzave_id')->index(); 
            $table->bigInteger('gradovi_id')->index(); 
            $table->bigInteger('poste_id')->index(); 

            $table->timestamps();

            $table->foreign('subjekti_id')->references('id')->on('subjekti')->onDelete('cascade');
            $table->foreign('drzave_id')->references('id')->on('drzave')->onDelete('SET NULL');
            $table->foreign('gradovi_id')->references('id')->on('gradovi')->onDelete('SET NULL');
            $table->foreign('poste_id')->references('id')->on('poste')->onDelete('SET NULL');
   
        });

Try 2

public function up()
  {
        Schema::create('subjekat_adrese', function (Blueprint $table) {
           
            $table->bigIncrements('id');
           
            $table->string('naziv');
            $table->string('adresa');

            $table->boolean('primarna')->unique();

            $table->bigInteger('subjekti_id')->index(); 
            $table->bigInteger('drzave_id')->index(); 
            $table->bigInteger('gradovi_id')->index(); 
            $table->bigInteger('poste_id')->index(); 

            $table->timestamps();

            
        });

        Schema::table('subjekat_adrese', function($table) {
            $table->foreign('subjekti_id')->references('id')->on('subjekti')->onDelete('cascade');
            $table->foreign('drzave_id')->references('id')->on('drzave')->onDelete('SET NULL');
            $table->foreign('gradovi_id')->references('id')->on('gradovi')->onDelete('SET NULL');
            $table->foreign('poste_id')->references('id')->on('poste')->onDelete('SET NULL');
        });
    }

I dont know what to try next but nothing not wotk.

CodePudding user response:

$table->foreignId('user_id')->constrained()->onDelete('cascade');

you tried it in this way to see ?? I mean this way instead of

$table->foreign('subjekti_id')->references('id')->on('subjekti')->onDelete('cascade');
  • Related