Laravel Foreign key Constraint incorrectly informed

Asked

Viewed 271 times

0

I couldn’t find where the bug is in my Migration, honestly.

Can anyone there understand why I’m making the following mistake:

SQLSTATE[HY000]: General error: 1005 Can’t create table imobiliaria.#sql-2d3c_21 (Rrno: 150 "Foreign key Constraint is incorrectly Formed") (SQL: alter table imoveis add Constraint imoveis_capa _foreign Foreign key (capa) References imagens (id) on delete no action on update no action)

Migration:

   <?php

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

class CreateImoveisTable extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $set_schema_table = 'imoveis';

    /**
     * Run the migrations.
     * @table Imoveis
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('imoveis');
        Schema::create($this->set_schema_table, function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('tipo_id')->unsigned();
            $table->string('nome');
            $table->float('valor');
            $table->integer('cidade_id')->unsigned();
            $table->integer('atributo_id')->unsigned();
            $table->text('descricao')->nullable();
            $table->integer('finalidade')->nullable();
            $table->float('area');
            $table->integer('quartos');
            $table->integer('banheiros');
            $table->integer('garagens');
            $table->integer('suites');
            $table->string('bairro')->nullable();
            $table->integer('capa')->unsigned()->nullable();

            $table->foreign('capa')->references('id')->on('imagens')
                ->onDelete('no action')
                ->onUpdate('no action');

            $table->foreign('tipo_id')
                ->references('id')->on('tipos')
                ->onDelete('no action')
                ->onUpdate('no action');

            $table->foreign('cidade_id')
                ->references('id')->on('cidades')
                ->onDelete('no action')
                ->onUpdate('no action');

            $table->foreign('atributo_id')
                ->references('id')->on('atributos')
                ->onDelete('no action')
                ->onUpdate('no action');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->set_schema_table);
     }
}

Migration of the images:

 <?php

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

class CreateImagensTable extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $set_schema_table = 'imagens';

    /**
     * Run the migrations.
     * @table Imagens
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->set_schema_table, function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('urlImagem', 45)->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->set_schema_table);
     }
}
  • Has how to put the full images Migration?

  • @Virgilionovic I put

  • The order that was wrong, that is, first you have to create the table of images, then create the table of immobles, because, the immovable one needs images to exist

  • But the order was right still, at the time of execution he executed first of images, then of real estate.

  • That was my test...

No answers

Browser other questions tagged

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