簡體   English   中英

Laravel 4 Migration - 無法添加外鍵約束

[英]Laravel 4 Migration - Cannot add foreign key constraint

這是我的遷移代碼:

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

當沒有注釋掉定義外鍵約束的代碼時,我在命令行上收到以下錯誤: 常規錯誤:1215無法添加外鍵約束

我有什么想法我做錯了嗎?

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

應該

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

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

$table->unsignedInteger('foo_id');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM