Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gargi369's avatar

String as foreign key doesn't work

Hello I'm trying create relations on 2 tables on string keys, that's my migrations:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVerticesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('vertices', function (Blueprint $table) {
            $table->string("name")->primary();
            $table->string("status");
            $table->integer("level");
        });

        Schema::table("vertices", function($table) {
            $table->foreign("name")->references("vertex_a")->on("edges");
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('vertices');
    }
}
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEdgesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('edges', function (Blueprint $table) {
            $table->increments("id");
            $table->string("vertex_a")->unique();
            $table->string("vertex_b");
            $table->integer("weight_a");
            $table->integer("weight_b")->nullable()->default(NULL);
            $table->integer("status");
        });
    }

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

When I turn on my migrations i get error:

[Illuminate\Database\QueryException]                                   
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constrain  
  t (SQL: alter table `vertices` add constraint `vertices_name_foreign`  
   foreign key (`name`) references `edges` (`vertex_a`))
0 likes
7 replies
d3xt3r's avatar

The table (foreign) must be created before the key could be added. So the order of migration is important ...

gargi369's avatar

I put it in other .php dile and still doesn't work with same error.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVerticesReferences extends Migration {

    public function up() {
        Schema::table("vertices", function($table) {
            $table->foreign("name")->references("vertex_a")->on("edges");
        });
    }

    public function down() {
    }
}
d3xt3r's avatar

Did you remove it from first file. ??

d3xt3r's avatar

No idea, try refreshing your migrations,

This works for me

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('vertices', function (Blueprint $table) {
            $table->string("name")->primary();
            $table->string("status");
            $table->integer("level");
        });

        Schema::create('edges', function (Blueprint $table) {
            $table->increments("id");
            $table->string("vertex_a")->unique();
            $table->string("vertex_b");
            $table->integer("weight_a");
            $table->integer("weight_b")->nullable()->default(NULL);
            $table->integer("status");
        });

        Schema::table("vertices", function($table) {
            $table->foreign("name")->references("vertex_a")->on("edges");
        });
    }
gargi369's avatar

Finally, if it isn't a good practice I will refactor my code to autoincrement field.

1 like

Please or to participate in this conversation.