Php 如何在迁移中删除表的外键和主键?
如何在迁移文件中删除多个外键和主键 下面是我的迁移文件代码 迁移文件Php 如何在迁移中删除表的外键和主键?,php,laravel,laravel-5,laravel-5.5,Php,Laravel,Laravel 5,Laravel 5.5,如何在迁移文件中删除多个外键和主键 下面是我的迁移文件代码 迁移文件 public function up() { Schema::create('role_user', function(Blueprint $table){ $table->integer('role_id')->unsigned(); $table->integer('user_id')->unsigned(); $table->fore
public function up()
{
Schema::create('role_user', function(Blueprint $table){
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
}
public function down()
{
Schema::drop('role_user');
//how drop foreign and primary key here ?
}
Blueprint类提供了dropForeign和dropPrimary方法,允许您删除外键约束和主键 下面应该可以做到这一点:
public function down()
{
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign('role_user_role_id_foreign');
$table->dropForeign('role_user_user_id_foreign');
$table->dropPrimary();
});
}
hi@jedzej我可以在dropForeign(['foreign_one','foreign_two])这样的单个dropForeign函数中删除两个外键吗;你不能。你为什么要这么做?只是为了在一个小的迁移文件中保存一行?