Home > Blockchain >  relationships in Laravel tables no id
relationships in Laravel tables no id

Time:08-09

I have two tables A and B Table A contains the site field, table B contains the site_id field I need to link these two tables by site field from table A.

            $table->bigInteger("site_id")->unsigned()->index();
            $table->foreign('site_id')
                ->references('site')
                ->on('data_args')
                ->onDelete('cascade');

getting this error - MySQL Error 1215: Cannot add foreign key constraint

CodePudding user response:

Let's say tables have the following fields:
table_a = (id, description)
table_b = (id, site_id, site_field)

Then you want to connect table_a and table_b, so on table_b:

$table->unsignedBigInteger('site_id')->nullable(false);
$table->foreign('site_id')->references('id')->on('table_a');

Use unsignedBigInteger() instead of bigInteger()->unsigned()

->references = the field to reference
->on = name of the table

CodePudding user response:

Two fields primary key and foreign key should be same data type site_id is bigInteger, the another table primary key should be bigInteger

  • Related