Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 8 General error: 1215 Cannot add foreign key constraint

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

$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’);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading