I have a problem using onDelete('set null')
in a foreign key. You are returning the error:
[Illuminate \ Database \ QueryException]
SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint (SQL: alter tableusers
add constraintusers_instituicoes_id_foreign
foreign key (instituicoes_id
) referencesinstituicoes
(id
) on delete set null)[PDOException]
SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint
If I change the set null
by cascade
it works.
I'm creating the migration like this:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('nome');
$table->string('user')->unique();
$table->string('email')->unique();
$table->string('password');
$table->integer('instituicoes_id')->unsigned();
$table->string('telefone')->nullable();
$table->string('img')->default('default.png');
$table->boolean('ativo');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function(Blueprint $table){
$table->foreign('instituicoes_id')
->references('id')
->on('instituicoes')
->onDelete('set null');
});