我想我的回滚数据库,但是有这样的错误:
[照亮\数据库\ QueryException] SQLSTATE [23000]:完整性约束违规:1217无法删除或更新父行,外键约束失败(SQL:DROP TABLE
tb_levels
)[PDOException] SQLSTATE [23000]:完整性约束违规:1217无法删除或更新父行,外键约束失败
这是我的移民代码:
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::create('tb_users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id_user');
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->integer('id_level')->unsigned();
$table->string('password', 60);
$table->rememberToken();
$table->boolean('activated')->default(false);
$table->timestamps();
$table->foreign('id_level')->references('id_level')->on('tb_levels');
});
Schema::enableForeignKeyConstraints();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::table('tb_users', function(Blueprint $table){
$table->dropForeign('tb_users_id_level_foreign');
$table->dropColumn('id_level');
});
Schema::drop('tb_users');
Schema::enableForeignKeyConstraints();
}
我尝试了几种方法,我在这个论坛中发现,但还是得到了错误,任何帮助吗?
首先禁止外键使用此:
SET FOREIGN_KEY_CHECKS = 1;
SET GLOBAL FOREIGN_KEY_CHECKS = 1;
然后迁移数据库。
再次申请外键约束:
SET FOREIGN_KEY_CHECKS = 0;
SET GLOBAL FOREIGN_KEY_CHECKS = 0;
好了,终于我找到了一种方法来解决这个错误,
首先你需要迁移到删除外键和列的,这是代码:
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::table('tb_users', function(Blueprint $table){
$table->dropForeign('tb_users_id_level_foreign');
$table->dropColumn('id_level');
});
Schema::enableForeignKeyConstraints();
}
然后迁移它,之后,它会删除列和外键。
之后删除架构::表的代码,保存它,然后运行命令:
php artisan migrate:reset
那么,它的工作原理,但它真是一个不切实际的方式,
希望在那里了,而比这更简单的方法。