我在運行遷移時遇到問題。我有一個MySQL數據庫與一些表。具體的表格是product_blender
。表中的某些字段是這樣的:Laravel 5遷移 - 外鍵約束失敗
- id (PK)
- area_id (FK)
- inhabitants (varchar)
- heating_type_id (FK)
- ...
現在我想創建一個名爲installateur_types
另一個表。該表需要包含PK和varchar字段。我還想創建一個FK在product_blender
表中的我的新創建tabel。
這是我做了什麼:
生成的遷移創建一個表:
public function up()
{
Schema::create('installateur_types', function(Blueprint $table)
{
$table->increments('id');
$table->string('type');
});
}
public function down()
{
Schema::drop('installateur_types');
}
運行遷移,這是成功的。表格是使用正確的字段創建的。
然後,我創建了遷移以將FK字段添加到product_blender表。
public function up()
{
Schema::table('product_blenders', function ($table) {
$table->integer('installateurtype_id')->unsigned();
$table->foreign('installateurtype_id')->references('id')->on('installateur_types')->onDelete('cascade');
});
}
public function down()
{
//
}
我在做什麼錯?
以前是否存在'product_blenders'表? – AndreL