8

Here's my migration code:

public function up()
{
    Schema::create('foos', function(Blueprint $table) {
        // Primary key
        $table->increments('id');

        // Standard
        $table->engine = 'InnoDB';
        $table->timestamps();
        $table->softDeletes();
    });

    Schema::create('bars', function(Blueprint $table) {
        // Primary key
        $table->increments('id');

        // Define foreign key
        $table->integer('foo_id')->unsigned;

        // Foreign key contraints
        // NOTE: causes "General error: 1215 Cannot add foreign key constraint"
        // $table->foreign('foo_id')->references('id')->on('foos');

        // Standard
        $table->engine = 'InnoDB';
        $table->timestamps();
        $table->softDeletes();
    });
}

public function down()
{
    Schema::drop('foos');
    Schema::drop('bars');
}

When the code to define the foreign key constraint is not commented out, I get the following error on the command line: General error: 1215 Cannot add foreign key constraint.

Any ideas what I am doing wrong?

1 Answer 1

15
$table->integer('foo_id')->unsigned;

should be

$table->integer('foo_id')->unsigned();

or you can use short version:

$table->unsignedInteger('foo_id');
Sign up to request clarification or add additional context in comments.

2 Comments

In the famous words of Homer, "Doh!". Thanks!
Don't beat yourself up - I can only answer your question because I did the exact same thing. :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.