What is the relationship problem in these tables?

Asked

Viewed 109 times

0

I’m using the Laravel and would like to make a relationship of table of municipios containing the estado_id. The estado_id will be related to the estado_id of table of estados.

Just when executing the query returned me the following error:

[Illuminate Database Queryexception] SQLSTATE[HY000]: General error: 1005 Can’t create table banco_rep.#sql-26f8_16b (Rrno: 150 "Foreign key constr aint is incorrectly Formed") (SQL: alter table municipios add Constraint municipios_estados_id_foreign Foreign key (estados_id) References estados (id) on delete)

[Pdoexception] SQLSTATE[HY000]: General error: 1005 Can’t create table banco_rep.#sql-26f8_16b (Rrno: 150 "Foreign key constr aint is incorrectly Formed")

Then I have the codes PHP, part where I run and create the bank:

<?php    
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;    
class CreateTableMunicipiosTable extends Migration
{
  /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    Schema::dropIfExists('municipios');
    Schema::create('municipios', function (Blueprint $table) {
      $table->increments('municipios_id');
      $table->integer('estados_id')->unsigned();
      $table->string('nome', 100);
      $table->char('cep',10);
      $table->foreign('estados_id')
      ->references('estados_id')
      ->on('estados')
      ->onDelete('cascade');
      $table->timestamps();
      $table->softDeletes();
    });
  }    
  /**
  * Reverse the migrations.
  *
  * @return void
  */
  public function down()
  {
    Schema::dropIfExists('municipios');
  }
}

I create the table bank states:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;    
class CreateTableEstadosTable extends Migration
{
  /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    Schema::dropIfExists('estados');
    Schema::create('estados', function (Blueprint $table) {
      $table->increments('estados_id');
      $table->string('nome', 100);
      $table->char('sigla', 2);
      $table->timestamps();
      $table->softDeletes();
    });
  }    
  /**
  * Reverse the migrations.
  *
  * @return void
  */
  public function down()
  {

    Schema::dropIfExists('estados');
  }
}

Model Estados:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Estados extends Model
{
  protected $fillable = ['estados_id','nome','sigla'];
  protected $dates = ['deleted_at'];

  public function municipios()
  {
    return $this->belongsTo('App\Municipios');
  }
}

Model Municipios:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Municipios extends Model
{
  protected $fillable = ['municipios_id','estados_id', 'nome', 'cep'];
  protected $dates = ['deleted_at'];
  public function estados()
  {
    return $this->hasMany('App\Estados');
  }
}
  • @Everson. How to remove the quotation marks? Quotation marks are not required?

  • Your two models are wrong because keys cannot be in the configuration $fillable yes in the configuration $primaryKey because you changed the default, but, the error problem is because the table already exists, it is not better you reset the Migration and run again?

1 answer

0


Make the following changes:

In the Model Municipalities

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Municipios extends Model
{
  protected $fillable = ['municipios_id','estados_id', 'nome', 'cep'];
  protected $dates = ['deleted_at'];
  public function estados()
  {
    return $this->hasMany('App\Estados','estados_id','estados_id');
  }
}

In the State Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Estados extends Model
{
  protected $fillable = ['estados_id','nome','sigla'];
  protected $dates = ['deleted_at'];

  public function municipios()
  {
    return $this->belongsTo('App\Municipios','estados_id','estados_id');
  }
}

Browser other questions tagged

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