2017-08-29 315 views
0

我知道這很容易被這樣實現:laravel 5.4添加索引字段

$table->increments('id'); 
$table->string('first_name'); 
$table->string('last_name'); 
$table->unsignedInteger('gender_id'); 
$table->date('date_of_birth'); 
$table->date('active_in_sport'); 
$table->unsignedInteger('people_type_id'); 
$table->timestamps(); 

$table->unique([ 
    'first_name', 
    'last_name' 
]); 
$table->index('gender_id'); 
$table->index('people_type_id'); 

這樣做的缺點是,Laravel首先創建表,比運行一個alter table查詢。 因爲這一點,我遇到了一個外鍵錯誤,所以我想將索引添加到create語句中的字段。我怎樣才能做到這一點?

+2

如果您剛剛創建表並且外鍵無法引用它,您將如何遇到外鍵錯誤? – Padarom

回答

1

你沒有任何外鍵,所以它可能是你的代碼的另一部分的問題。

雖然你可以做一個內聯索引,而不是在遷移結束時。例如,$table->unsignedInteger('gender_id')->index()。如果你喜歡鏈式語法,你可以進一步與$table->integer('gender_id')->unsigned()->index()