I’m not able to create the relationship between the tables through the migrations of Laravel

Asked

Viewed 274 times

0

I am using the Multi Tenancy in Laravel, so I created the Migrations, without relationship between table was working normal, when I related that started the error.

I thought it was the execution order of the migrations, so I renamed it to execute in the correct form, but the error continued.

Migrations -> 2019_08_07_024917_list

    public function up()
    {
        Schema::create('tbd_lista', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nome')->nullable();
            $table->timestamps();
        });
    }

Migrations -> 2019_08_08_024935_card

public function up()
    {
        Schema::create('tbd_cartao', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nome',100)->nullable();
            $table->integer('tbd_lista_id')->unsigned();
            $table->foreign('tbd_lista_id')->references('id')->on('tbd_lista');
            $table->longText('descricao');
            $table->boolean('arquivar');    
            $table->dateTime('data');
            $table->timestamps();
        });
    }

Error:

"SQLSTATE[HY000]: General error: 1215 Cannot add Foreign key Constraint (SQL: alter table tbd_cartao add Constraint tbd_cartao_tbd_lista_id_foreign Foreign key (tbd_lista_id) References tbd_lista (id))"

  • I am also studying Latin :v I have to go through something similar. Next try $table->integer('tbd_lista_id')->unsigned(); for $table->bigInteger('tbd_lista_id')->unsigned();. maybe you need to erase this table from the bank in hand.

  • Thank you very much, it worked!!!

4 answers

0

0


How you ta using bigIncrements you need to pass in the other table bigInteger follows its own changed code:

public function up()
    {
        Schema::create('tbd_cartao', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nome',100)->nullable();
            $table->bigInteger('tbd_lista_id')->unsigned();
            $table->foreign('tbd_lista_id')->references('id')->on('tbd_lista');
            $table->longText('descricao');
            $table->boolean('arquivar');    
            $table->dateTime('data');
            $table->timestamps();
        });
    }

0

trade in $table->integer('tbd_lista_id')->unsigned(); for $table->bigInteger('tbd_lista_id')->unsigned(); to remain identical to the key Primary of the related table.

-1

I have already gone through this several times and my solution was to backup the tables delete the database and run php Artisan Migration again, why for some reason Laravel does not accept new tables with Foreign key.

  • He accepts, but he expects something smarter, try to integer for bigInteger impossible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.