Relationship tables laravel

2

Good morning

I have 2 tables clients and companies, in the second table I am trying to create the relationship but I get an error with the foreign key.

  

Illuminate \ Database \ QueryException: SQLSTATE [HY000]: General error:   1005 Can not create table usaly_bd . #sql-1d3c_2bb (errno: 150   "Foreign key constraint is incorrectly formed") (SQL: alter table    companies add constraint companies_ client_id_foreign foreign key   ( client_id ) references clients ( company_id ))

This is my clients migration

 public function up()
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->increments('client_id');
            $table->integer('nuip')->nullable();
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->integer('phone')->nullable();
            $table->integer('cellphone')->nullable();
            $table->string('email')->nullable();
            $table->string('type_client')->nullable();
            $table->timestamps();


        });
    }

Migration companies

public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('company_id')->unsigned();
            $table->integer('name')->nullable();
            $table->string('business_name')->nullable();
            $table->string('douments')->nullable();
            $table->timestamps();

            //Relacion
            $table->unsignedInteger('client_id');
            $table->foreign('client_id')->references('company_id')->on('clients');


        });
    }

I appreciate your help

    
asked by Daniel Lopez 03.01.2019 в 03:09
source

3 answers

1

You have the wrong column that your FOREIGN KEY refers to

currently it's like this

$table->unsignedInteger('client_id');
$table->foreign('client_id')->references('company_id')->on('clients');

But it should be like this

$table->unsignedInteger('client_id');
$table->foreign('client_id')->references('client_id')->on('clients');
  

Since the foreign key in your table companies is pointing to   the table clients then there the relationship did not have it correct and   As you have it in your question, your foreign key is wrong because it points   to the primary key but of the same table that is containing it

Documentation reference Foreign keys

Discuss how it went

    
answered by 03.01.2019 / 03:18
source
0

Dear.

To solve this problem in Laravel can be done as follows.

  • First, you must create a migration where you only create table fields.
  • Second, a new migration must be created to add foreign keys to that table.

In this case this way:

Migration Clients Keep the same. Create a Migration called "create_companies_table"

public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('company_id')->unsigned();
            $table->integer('name')->nullable();
            $table->string('business_name')->nullable();
            $table->string('douments')->nullable();
            $table->timestamps();

            //campo con el index para la relacion
            $table->integer('client_id')->unsigned()-index('client_id_foreign');


        });
    }

A second migration called "add_fk_to_companies_table"

public function up()
{
    Schema::table('companies', function(Blueprint $table)
    {
       //crea la clave foranea hacia la tabla clients
        $table->foreign('client_id')->references('id')->on('clients')->onUpdate('RESTRICT')->onDelete('RESTRICT');

    });
}

I hope it's useful !!!

Be sure to respect the chronological order of migrations, first create the table, then create the relationship.

    
answered by 03.01.2019 в 21:05
0

Welcome Daniel Lopez, the error is that you are linking your foreign key with an element that does not exist in the clients table. Another thing is that your primary key of the clients table has the same name as the foreign key of the companies table, so the solution would be this:

 $table->foreign('client_id')->references('client_id')->on('clients');
    
answered by 03.01.2019 в 21:21