How to resolve foreign key error 150 in Migration with Laravel?

Asked

Viewed 640 times

4

I’m having trouble creating the foreign key in Migration.

I am working with PHP, LARAVEL 5.3 and MYSQL. You’re making the following mistake:

inserir a descrição da imagem aqui

Below is my code:

Table Migration categs

class CreateCategsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categs', function (Blueprint $table) {
            $table->increments('id');
            $table->text('nome_cat');
            $table->timestamps();
        });
    }

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

Migration from the Products Table

class CreateProdutosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('produtos', function (Blueprint $table) {
            $table->increments('id');
            $table->text('nome');
            $table->text('descricao');
            $table->integer('categs_id');
            $table->foreign('categs_id')->references('id')->on('categs');
            $table->timestamps();
        });
    }

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

Model categs

use Illuminate\Database\Eloquent\Model;

class categ extends Model
{
    //Relacionamento 1:n Categoria dos produtos

    public function produtos()
    {
        return $this->hasMany('App\produtos');
    }
}

Model products

use Illuminate\Database\Eloquent\Model;

class Produtos extends Model
{
    //
    public function categoria()
    {
        return $this->belongsTo('App\categ');
    }
}
  • Take the simple quotes from FK’s name

  • I took it off, and it didn’t work.

  • do it this way and check if it worked. $table->integer('categs_id')->unsigned()

  • Tip: It’s a bad idea to put Foreign Keys together with field creation. By default, always use a Migration for creating fields and another for creating Foreign Keys.

3 answers

3


When running the Seed command. The Laravel adopts the order of the files as the top-down execution line. That is, the migrate of categs must be before the migrate of produtos.

For example: the tables Companies has relationship 1-N with Banks, soon the migrate of Companies must be above the migrate Banks.

inserir a descrição da imagem aqui

Schema::create('banks', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->integer('account');
    $table->integer('bank_agency');
    $table->integer('company_id')->unsigned(); //compaines_table.php
    $table->timestamps();

    $table->foreign('company_id')->references('id')->on('companies');
});
  • I changed the date of Migration categs in order to stay above Migration products, even so, continues the same error.

2

I was able to solve the problem as follows: no migration products, modified from

$table->integer('categs_id'), 

for

$table->unsignedinteger('categs_id')

0

Another way to create the relationship field:

$table->integer('categs_id', false, true);

Browser other questions tagged

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