0

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

user3581438
  • 350
  • 1
  • 4
  • 8
  • try this `$table->integer('user_id')->nullable()->unsigned();` – Reza sh Mar 12 '19 at 05:56
  • Check this out https://stackoverflow.com/a/52902308/5928015 ? if your using `bigIncrements` then you need to handle that accordingly – Vipertecpro Mar 12 '19 at 07:22
  • When you use syntax or functionality, read the manual on how to use it. Before considering posting please always google many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. But this is obviously a duplicate. [ask] Also please tag questions with the SQL DBMS you are using. – philipxy Mar 12 '19 at 10:11
  • Possible duplicate of [MySQL Error 1215: Cannot add foreign key constraint](https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – philipxy Mar 12 '19 at 10:17

3 Answers3

2

Try this

Schema::create('repositories', function (Blueprint $table) {
     $table->string('id', 8)->primary();
     $table->string('name')->nullable(false);
     $table->string('size')->nullable(false);
     $table->unsignedBigInteger('user_id');
     $table->timestamps();

     $table->foreign('user_id')->references('id')->on('users');
});

According to laravel 5.8 Foreign Key Constraints

Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');

    $table->foreign('user_id')->references('id')->on('users');
});
bhavinjr
  • 1,663
  • 13
  • 19
1

Starting with Laravel 5.8, bigIncrements is used instead of increments for primary keys. If you are using bigIncrements for your primary key you must declare the user_id field as bigInteger instead of integer. In this way the field of the primary key and the foreign key will share the same type of data, otherwise you will get an error

Nelson Aguiar
  • 69
  • 1
  • 2
0

Its probably because the user_id column does not match the id column.

user_id needs to be of type bigInteger and also be indexed.

Ref: Setting a foreign key bigInteger to bigIncrements in Laravel 5.4

martinethyl
  • 184
  • 1
  • 12