2015-12-06 99 views
6

我需要使用外鍵爲我的數據庫,但我不能這樣做,在命令行中運行遷移命令後,我得到這個錯誤:如何使用外鍵laravel 5.1遷移

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table samples add constraint s amples_supplier_id_foreign foreign key (supplier_id) references suppliers (id))

[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

樣品遷移:

Schema::create('samples', function (Blueprint $table) { 
      $table->Increments('id',true); 
      $table->string('variety',50); 
      $table->integer('supplier_id')->unsigned(); 
      $table->foreign('supplier_id')->references('id')->on('suppliers'); 
      $table->string('lot_number'); 
      $table->date('date'); 
      $table->integer('amount'); 
      $table->integer('unit_id')->unsigned(); 
      $table->foreign('unit_id')->references('id')->on('unit'); 
      $table->string('technical_fact'); 
      $table->string('comments'); 
      $table->string('file_address'); 
      $table->integer('category_id')->unsigned(); 
      $table->foreign('category_id')->references('id')->on('category'); 
      $table->timestamps(); 
     }); 

供應商遷移:

Schema::create('suppliers', function (Blueprint $table) { 
      $table->Increments('id',true); 
      $table->string('supplier',50); 
      $table->timestamps(); 
     }); 

我嘗試與樣品新的遷移,但unsucce ssful:

Schema::create('samples', function (Blueprint $table) { 
       $table->Increments('id',true); 
       $table->string('variety',50); 
       $table->integer('supplier_id')->unsigned(); 
       $table->string('lot_number'); 
       $table->date('date'); 
       $table->integer('amount'); 
       $table->integer('unit_id')->unsigned(); 
       $table->string('technical_fact'); 
       $table->string('comments'); 
       $table->string('file_address'); 
       $table->integer('category_id')->unsigned(); 
       $table->timestamps(); 
     }); 

     Schema::table('samples', function($table) { 
      $table->foreign('supplier_id')->references('id')->on('suppliers'); 
      $table->foreign('unit_id')->references('id')->on('unit'); 
      $table->foreign('category_id')->references('id')->on('category'); 
     }); 

我嘗試修復主鍵的長度爲10,但不成功再

回答

4

訂單事宜。

在嘗試將該表上的列引用爲約束之前,您想確保您的「供應商」表存在。

所以如果你想設置你的外鍵約束而創建表,則請確保您所創建的「供應商」遷移第一,「樣本」遷移算賬:

php artisan make:migration create_suppliers_table --create=suppliers 
php artisan make:migration create_samples_table --create=samples 

。 ..將架構代碼添加到您的遷移文件。然後:

php artisan migrate 

如果你不想擔心在其中創建表,那麼請您先創建表遷移的順序,沒有外鍵約束,然後做一個額外的移民加你的外鍵。

php artisan make:migration create_samples_table --create=samples 
php artisan make:migration create_suppliers_table --create=suppliers 
php artisan make:migration alter_samples_table --table=samples <-- add your foreign key constraints to this migration file 

...將模式代碼添加到您的遷移文件。然後:

php artisan migrate 
1

嘗試像這樣

Schema::table('samples', function($table) { 
     $table->integer('supplier_id')->unsigned(); 
     $table->foreign('supplier_id')->references('id')->on('suppliers'); 
     $table->integer('unit_id')->unsigned(); 
     $table->foreign('unit_id')->references('id')->on('unit'); 
     $table->integer('category_id')->unsigned(); 
     $table->foreign('category_id')->references('id')->on('category'); 
    }); 
+0

我說的做,但unsuccessfull –

+1

首先執行的供應商遷移,然後品嚐migration.Cause沒有供應商遷移supplier_id將不能作爲外鍵 –

2

最後生成的表牢記遷移如果你覺得任何diffuculties只是爲了你的名字,他們應該是有序的table_foreign_keys

Schema::table('samples', function($table) { 
      $table->foreign('supplier_id')->references('id')->on('suppliers'); 
      $table->foreign('unit_id')->references('id')->on('unit'); 
      $table->foreign('category_id')->references('id')->on('category'); 
     }); 

最後將所有與此相關的外鍵加入並運行

0

KorreyD說的是真的! ,但我創建遷移,然後改名遷移到重新排序,它是如此簡單:

前改名爲:

供應商遷移:2015_08_ _104217_supllier_table.php

樣品遷移:2015_08_ _102325_samples_table。PHP

改名後:

樣品遷移:2015_08_ _102325_samples_table.php

供應商遷移:2015_08_ _104217_supllier_table.php

我的問題解決了!因爲樣品遷移之前的供應商遷移運行

評論:我嘗試用這種反射,即更名爲任何地方所使用的遷移名

+0

我一直在努力與這些事情。看看laravelsd.com。您可以在那裏創建一個數據庫結構,然後將其全部導出。它創建遷移並將所有密鑰綁定放在最後一個文件中。 –

+0

太棒了!是的,這也可以。我沒有提到這種方法,因爲我不是laravel專家,我不確定可能因重命名遷移文件而出現任何問題。 – KorreyD