Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

paboo's avatar

1215 Cannot add foreign key constraint

i've tried many time to create my relationship in laravel. Error 1215 make me die


 <?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
       Schema::create('users',function($t){
         $t->engine = "InnoDB";
        $t->increments('id');
        $t->string('email',30);
        $t->string('name',30);
        $t->string('age',2);
        $t->string('marry_status');
        $t->timestamps('timecreated');
       });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePointsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('points',function($t){
             $t->engine = "InnoDB";
        $t->increments('id');
        $t->double('points');
        $t->integer('users_id')->unsigned();
         $t->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $t->timestamps('timecreated');
       });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
       Schema::drop('points');
    }
}

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAddresssTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addresss',function($t){
             $t->engine = "InnoDB";
        $t->increments('id');
        $t->double('address');
        $t->integer('users_id')->unsigned();
         $t->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $t->timestamps('timecreated');
          });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('addresss');
    }
}

0 likes
13 replies
Kryptonit3's avatar

@paboo

As far as foreign keys in your migrations are concerned, everything looks fine. I would like to mention something about your timestamps though. Laravel has a built in timestamp function. So you should do this instead:

public function up()
{
    Schema::create('users', function($table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('email',30);
        $table->string('name',30);
        $table->string('age',2);
        $table->string('marry_status');
        $table->timestamps();
    });
}

Then you can call $user->created_at; and $user->updated_at; and display it in relative terms with $user->created_at->diffForHumans();

Also, I see you are using 2 space tabbing/inconsistant tab/spaces. Laravel 5 follows PSR-2 which defines Tabs as 4 spaces. Not like it will cripple your app or anything. :)

Ok, that is out of the way. Now, how do your models look? Post those so we can see. Also, show us how you are calling your relations. You say you are having a foreign key issue? Can you post the errors?

For future reference though, you can have a boilerplate migration generated with an artisan command.

php artisan make:migration create_points_table --create=points
1 like
jekinney's avatar

Drop the unsigned and try again. Also you may want to try putting the foreign key code after the timestamps, not sure if that part will help, but I always do it that way.

That error code triggers when they type is different then the main column, but you have it as integer so probably the unsigned.

Kryptonit3's avatar

@jekinney - Without the ->unsigned() she will definitely run into issues. The foreign keys are fine being defined right after the column definition btw. :)

jekinney's avatar

@Kryptonit3 yeah it was a guess, I personally don't set my foreign keys to unsigned (0 or positive numbers only) as the key restraint makes sure the int is valid in the other table! Good to know though! Never stop learning! :)

1 like
Kryptonit3's avatar

@jekinney here is a sample migration I use on LaraBin

    public function up()
    {
        Schema::create('bin_version', function (Blueprint $table) {
            $table->bigInteger('bin_id')->unsigned();
            $table->foreign('bin_id')->references('id')->on('bins')->onUpdate('cascade')->onDelete('cascade');
            $table->bigInteger('version_id')->unsigned();
            $table->foreign('version_id')->references('id')->on('versions')->onUpdate('cascade')->onDelete('cascade');
        });
    }

I see she marked your answer as correct, curious how dropping the unsigned() (that references an auto increment index) fixed the issue. hmm...

btw, I have started using bigInteger and bigIncrements in my projects so I don't have to worry 20 years down the road about ids running out and the performance hit is miniscule.

1 like
paboo's avatar

it gone. i create table first then set foreign key >.<

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePointsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('points',function(Blueprint $t){
             $t->engine = "InnoDB";
        $t->increments('id');
        $t->double('points');
        $t->integer('user_id')->unsigned();
        // $t->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $t->timestamps('timecreated');
       });
         Schema::table('points',function(Blueprint $t){
           
               $t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
       Schema::drop('points');
    }
}

jekinney's avatar

@Kryptonit3 love your site by the way! Other then what I stated, have no idea. Never tried it.

2 likes
Kryptonit3's avatar

@paboo - that is not necessary. You can define keys while creating the table. Your issue has to be related to something else.

1 like
paboo's avatar

meaning foreign same type i using auto_increment. so what's type foreign need @Kryptonit3

Kryptonit3's avatar
Level 11
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function($t) {
            $t->engine = "InnoDB";
            $t->increments('id');
            $t->string('email',30);
            $t->string('name',30);
            $t->string('age',2);
            $t->string('marry_status');
            $t->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePointsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('points', function($t) {
            $t->engine = "InnoDB";
            $t->increments('id');
            $t->double('points');
            $t->integer('user_id')->unsigned();
            $t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $t->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('points');
    }
}
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAddresssTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addresss',function ($t) {
            $t->engine = "InnoDB";
            $t->increments('id');
            $t->double('address');
            $t->integer('user_id')->unsigned();
            $t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $t->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('addresss');
    }
}

This is how they should look.

I noticed your foreign key columns were called users_id, Laravel uses a particular naming schema when referencing relationships on models and if not followed, you will need to manually reference the columns in your relationship call.

Post the models for these tables.

2 likes

Please or to participate in this conversation.