Reputation: 6742
I get the error:
SQLSTATE[HY000]: General error: 1005 Can't create table
monkeybussiness
.#sql-db0_17
(errno: 150 "Foreign key co nstraint is incorrectly formed")
while the columns are identical as far as I know. Why is this happening?
Events migrations file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEventsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('events', function (Blueprint $table) {
$table->increments('afspraak_id');
$table->timestamps();
$table->string('titel');
$table->integer('persoon_id');
$table->foreign('persoon_id')->references('persoon_id')->on('personen');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('events');
}
}
Personen migrations file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Personen extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('personen', function (Blueprint $table) {
$table->increments('persoon_id');
$table->timestamps();
$table->string('voornaam');
$table->string('achternaam');
$table->string('email')->unique();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
I tried ->unsigned()
but that doesn't work either.
Upvotes: 0
Views: 542
Reputation: 6742
The problem was that the Events table was created first. I had to change the date of the filename so that it would be after the persoon
table. After that it worked.
Upvotes: 1
Reputation: 13259
It seems you have copied pasted without editing your migration files. Your personen
migrations has all the same data as 'events'. If that's the case in your application, then surely the foreign key cannot work because the table does not exist.
If it just an error of copy, make sure you put the correct table name here
$table->foreign('persoon_id')->references('persoon_id')->on('personen');
//table name should be 'personens' ?
Can you try to alias it?
$table->foreign('persoon_id', 'fk_persoon_id')->references('persoon_id')->on('personen')
->onDelete('no action')
->onUpdate('no action');
Upvotes: 0