我想在一个表上创建几个带FK的表。
在迁移中使用up
方法创建表时失败的外部约束。
我应该使用这样的代码
public function up()
{
Schema::create('Pharmacy', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
});
if (Schema::hasTable('Drug')) {
Schema::table('Drug', function (Blueprint $table) {
$table->foreign('pharmacy_id')->references('id')->on('Pharmacy');
});
}
if (Schema::hasTable('Address')) {
Schema::table('Address', function (Blueprint $table) {
$table->foreign('pharmacy_id')->references('id')->on('Pharmacy');
});
}
}
或者创建新的迁移?
我是否应该在每个表和外键上使用新的迁移,并且可以使用一次迁移来创建所有表,另一次迁移是否可以添加FK?