0

I am trying to create the tables 'teams' and 'competitions' in laravel but when I run the migrate command, I get the following: errno: 150 "Foreign key constraint is incorrectly formed"

Schema::create('competitions', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name')->unique();
                    $table->string('team_name');
                    $table->foreign('team_name')->references('name')->on('teams')->onDelete('cascade');
                    $table->timestamps();
        });

Schema::create('teams', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name')->unique();
                    $table->string('place');
                    $table->string('competition')->nullable();;
                    $table->foreign('competition')->references('name')->on('competitions')->onDelete('set null');
                    $table->timestamps();
});
jordibenck
  • 175
  • 1
  • 4
  • 15

1 Answers1

0

You must use same properties for primary key and foreign key

eg. both had ->nullable() or not.

Also, make sure the collation is same for two tables and their columns.

MohamedSabil83
  • 1,529
  • 7
  • 12