I am trying to make a foreign key to "users" table using Laravel 5.8.
Laravel 5.8 auto generated migration table is as follows,
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Then from my "repositories" table I am referencing "users" table as follows,
Schema::create('repositories', function (Blueprint $table) {
$table->string('id', 8)->primary();
$table->string('name')->nullable(false);
$table->string('size')->nullable(false);
$table->unsignedInteger('user_id')->nullable(false);
$table->timestamps();
});
Schema::table('repositories', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
But I am getting "General error: 1215 Cannot add foreign key constraint" error on this code. There were many solutions related to this problem.
General error: 1215 Cannot add foreign key constraint in laravel
Migration: Cannot add foreign key constraint in laravel
Laravel migration "Cannot add foreign key constraint" error with MySQL database
I tried the above solutions already. But my problem was not solved