2013-06-04 56 views
5

我在一個新的Laravel 4項目中進行了幾次移植。一個是地區,另一個是地區。每個地區都有多個地區,地區屬於地區。Laravel 4移植拋出1072錯誤

我已經在很多場合使用過Laravel 4和遷移函數,但之前從未遇到過這個問題。當我運行php artisan migrate:install其次是php artisan migrate我得到以下錯誤:

$ php artisan migrate 
    [Exception] 
    SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'region_ 
    id' doesn't exist in table (SQL: alter table `areas` add constraint areas_r 
    egion_id_foreign foreign key (`region_id`) references `regions` (`id`)) (Bi 
    ndings: array (
)) 
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="... 
"]] [--pretend] [--seed] 

//該地區遷移

class CreateRegionsTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Creates the regions table 
    Schema::create('regions', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->string('name', 160)->unique(); 
     $table->timestamps(); 
    }); 
    } 
} 

//該地區遷移

class CreateAreasTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Creates the cemeteries table 
    Schema::create('areas', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->foreign('region_id')->references('id')->on('regions'); 
     $table->string('name', 160)->unique(); 
     $table->timestamps(); 
    }); 
    } 
} 

回答

17

你必須創建列相關的外鍵:

class CreateAreasTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Creates the cemeteries table 
    Schema::create('areas', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 

     $table->integer('region_id')->unsigned(); 
     $table->foreign('region_id')->references('id')->on('regions'); 

     $table->string('name', 160)->unique(); 
     $table->timestamps(); 

    }); 
    } 
} 

有時候(取決於你的數據庫服務器上),你必須在兩個步驟來創建外鍵:

class CreateAreasTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Create the table and the foreign key column 
    Schema::create('areas', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 

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

     $table->string('name', 160)->unique(); 
     $table->timestamps(); 

    }); 

    // Create the relation 
    Schema::tabe('areas', function($table) 
    { 
     $table->foreign('region_id')->references('id')->on('regions'); 
    }); 
    } 
} 
+0

啊,這很有趣。所以$ table-> foreign('columnname')...不會創建一個基於整數的列,如果它不存在? – JasonMortonNZ

+0

就是這樣。它只是創建表之間的關係。 –

+0

解決了我收到的錯誤,但用另一個替換了它。 [異常] SQLSTATE [HY000]:常規錯誤:1005無法創建表 'NZF-組織#SQL-107c_ C8'(錯誤:150)(SQL:ALTER TABLE'areas'添加約束areas_region_id_f 的外國人外鍵('region_id')引用'區域'('id'))(綁定:ar ray( )) – JasonMortonNZ

4

,不要忘了讓外國鍵無符號。

  $table->integer('region_id')->unsigned(); 
+0

爲什麼我需要使我的外鍵無符號?這是做什麼的,有什麼好處? – JasonMortonNZ

+3

這是一個laravel文檔建議: 注意:當創建引用遞增整數的外鍵時,請記得總是使外鍵列無符號 主鍵不能是負數,所以指向它們的外鍵也應該是未簽名。這也允許您可以在該列中使用的鍵數增加一倍。 –