I'm trying to create two related tables. When attempting to 'migrate' them, the error is returned: "SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint (SQL: alter table 'companies' add constraint companies_taxregimeid_foreign' foreign key ('taxRegimeId') references' tax_regimes' ('id') on delete CASCADE) / p>
Company - Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
protected $table = 'companies';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'taxRegimeId',
'managerId',
];
public function manager()
{
return $this->belongsTo('App\User', 'managerId', 'id');
}
public function taxRegime()
{
return $this->belongsTo('App\TaxRegime','taxRegimeId', 'id');
}
}
Company - Migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->integer('taxRegimeId')->unsigned();
$table->integer('managerId')->unsigned();
$table->timestamps();
});
Schema::table('companies', function (Blueprint $table) {
$table->foreign('managerId')->references('id')->on('users')->onDelete('CASCADE');
});
Schema::table('companies', function (Blueprint $table) {
$table->foreign('taxRegimeId')->references('id')->on('tax_regimes')->onDelete('CASCADE');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('companies');
}
}
As you can see in the model, I did as in the 'managerId' that follows the same scheme regarding the relationship of entities (A company has a manager | A company has a tax regime | taxRegime). In the 'managerId' works correctly, but in 'taxRegimeId' gives this problem I described at the beginning. What could have caused this problem?