I'm having some trouble with migrations in Laravel. I keep getting the error that the foreign key constraint is illformed. I can't seem to see what I do wrong either. Naming the migrations so they run in the right order is in place. I also tried running one and one migration, and it always stops on the second migration (municipalities).
What am I doing wrong here?
1st table: counties
public function up()
{
Schema::create('counties', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->string('name');
$table->timestamps();
});
DB::statement('ALTER TABLE counties CHANGE id id INT(2) UNSIGNED ZEROFILL NOT NULL');
}
2nd table: municipalities
public function up()
{
Schema::create('municipalities', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->bigInteger('county_id')->unsigned();
$table->foreign('county_id')->references('id')->on('counties')->onDelete('cascade');
$table->string('name');
$table->timestamps();
});
DB::statement('ALTER TABLE municipalities CHANGE county_id county_id INT(2) UNSIGNED ZEROFILL NOT NULL');
DB::statement('ALTER TABLE municipalities CHANGE id id INT(4) UNSIGNED ZEROFILL NOT NULL');
}
3rd table: postals
public function up()
{
Schema::create('postals', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->bigInteger('municipality_id')->unsigned();
$table->foreign('municipality_id')->references('id')->on('municipalities')->onDelete('cascade');
$table->string('name');
$table->timestamps();
});
DB::statement('ALTER TABLE postals CHANGE municipality_id municipality_id INT(4) UNSIGNED ZEROFILL NOT NULL');
DB::statement('ALTER TABLE postals CHANGE id id INT(6) UNSIGNED ZEROFILL NOT NULL');
}
4th table: zips
public function up()
{
Schema::create('zips', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->bigInteger('postal_id')->unsigned();
$table->foreign('postal_id')->references('id')->on('postals')->onDelete('cascade');
$table->decimal('lat', 12, 7);
$table->decimal('lon', 12, 7);
$table->timestamps();
});
DB::statement('ALTER TABLE zips CHANGE id id INT(4) UNSIGNED ZEROFILL NOT NULL');
DB::statement('ALTER TABLE zips CHANGE postal_id postal_id INT(6) UNSIGNED ZEROFILL NOT NULL');
}