【发布时间】:2016-01-20 06:20:34
【问题描述】:
我在运行迁移时遇到问题。我有一个带有一些表的 mysql 数据库。具体表为product_blender。表中有些字段是这样的:
- id (PK)
- area_id (FK)
- 居民(varchar)
- heater_type_id (FK)
- ...
现在我想创建另一个名为installateur_types 的表。该表需要包含一个 PK 和一个 varchar 字段。我还想在 product_blender 表中创建一个 FK 到我新创建的表的 id。
这就是我所做的:
已创建迁移以创建表:
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表以前是否存在?
标签: mysql database laravel laravel-5 foreign-keys