8

这是我的迁移代码:

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');
}

当定义外键约束的代码没有被注释掉时,我在命令行上得到如下错误:General error: 1215 Cannot add foreign key constraint

任何想法我做错了什么?

4

1 回答 1

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

应该

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

或者您可以使用简短版本:

$table->unsignedInteger('foo_id');
于 2013-09-25T02:54:57.180 回答