Olá, eu estouEstou usando o LARAVELLaravel
e gostaria de fazer um relacionamento da tabelatabela de MUNICÍPIOSmunicipios
QUEque contém o ESTADO_IDestado_id
. O ESTADO_IDestado_id
será relacionado com o ESTADO_IDestado_id
da tabelatabela de ESTADOSestados
.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `banco_rep`.`#sql-26f8_16b` (errno: 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 cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `banco_rep`.`#sql-26f8_16b` (errno: 150 "Foreign key constr
aint is incorrectly formed")
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table
banco_rep
.#sql-26f8_16b
(errno: 150 "Foreign key constr aint is incorrectly formed") (SQL: alter tablemunicipios
add constraintmunicipios_estados_id_foreign
foreign key (estados_id
) referencesestados
(id
) on delete cascade)
[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table
banco_rep
.#sql-26f8_16b
(errno: 150 "Foreign key constr aint is incorrectly formed")
Logo tenho os códigos PHP.
PartePHP
, parte na qual executo e crio o BDbanco:
<?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');
}
}
CRIO O BD dos estados:Crio o banco da tabela estados:
<?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:Model Estados:
MODEL MUNICÍPIOS:Model Municipios: