11

I am trying to mimic wordpress' primary key size which is BIGINT(20) but it seems that laravel doesn't have a native function to do this.. I saw a page in the laravel forums and got a code like this:

$table->bigInteger('id')->primary();

but when i try to attach a foreign key to that id during artisan migrate, there is a MYSQL error that is thrown:

[Exception] SQLSTATE[HY000]: General error: 1005 Can't create table 'db.#sql- 1730_15' (errno: 150) (SQL: alter table users add constraint users_role_id_foreign foreign key (role_id) references roles (id)) (Bindings: array ( ))

What is the proper way to do this or where do i get this thing wrong?

thanks!

1
  • 2
    This is not laravel issue, it's related to foreignkey, the column pair of foreignkey and it's primarykey must be of same type, you can remove primarykey, change type of both columns and set primarykey back Commented Jul 28, 2013 at 3:57

2 Answers 2

32

You most likely forgot to also set the type of your role_id foreign key as BIGINT(20) as well. This isn't really a Laravel issue, but rather MySQL's.


By the way, Laravel does have a native function to do this:

$this->bigIncrements('id');

This takes care of making it unsigned, auto increment and primary key.

7
  • Hi, seems ->primary() doesn't make it an auto-increment key.. maybe that's why. now my question is how should i have done it with laravel? Commented Jul 28, 2013 at 4:34
  • Found the native function for this. Updated the answer.
    – rmobis
    Commented Jul 28, 2013 at 6:18
  • yeah saw it already too (see here). was about to answer my own question but you edited it before me.. the problem is the error persists even if i use $this->bigIncrements('id'); on both users and roles tables.. Commented Jul 28, 2013 at 6:20
  • and yes it is now a native function, just not yet documented on the main site.. it is on the API but not on the docs yet Commented Jul 28, 2013 at 6:21
  • 1
    oh, nvm.. found it while i was copy pasting my code to the question.. yes it is indeed a type mismatch. didn't realize i was still using $table->unsignedInteger('role_id');, i have updated it to $table->unsignedBigInteger('role_id'); now. thanks a lot! Commented Jul 28, 2013 at 6:33
6

When using bigInteger() also applying it to foreign key in some table, make sure you connect it properly with unsignedBigInteger(),

public function up()
{
        Schema::create('create_this_table_after_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
           // Other Columns
        });
        Schema::table('create_this_table_after_users', function($table) {
            $table->foreign('user_id')->references('id')->on('users');
            // Other Constraints 
        });
}

Reference Link of the Laravel 4.2 Doc

3
  • 1
    Thanks, mate! I was searching why I keep getting this error General error: 1215 Cannot add foreign key constraint. Commented Feb 28, 2019 at 15:09
  • :) Glade i helped you.. Happy Coding Commented Feb 28, 2019 at 16:31
  • 1
    Oh man after so many tries with my migration in Laravel 5.8. This helped a lot.
    – wobsoriano
    Commented Aug 8, 2019 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.