SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table `users`)
我知道这已经被回答了好几次,但我仍然无法弄清楚我的迁移有什么问题。
首先我打电话给合作伙伴迁移
public function up()
{
Schema::create('partners', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('admin_id')->unsigned();
$table->string('company_name', 50);
...
}
public function down()
{
Schema::drop('partners');
}
然后我调用用户迁移
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('partner_id')->unsigned();
$table->string('email', 70)->unique();
$table->string('first_name', 50);
$table->string('last_name', 50);
$table->string('password', 60);
$table->string('image', 200)->nullable();
$table->string('gender', 10)->nullable();
$table->string('phone', 25)->nullable();
$table->string('nationality', 50)->nullable();
$table->string('address', 200)->nullable();
$table->boolean('is_active')->default(0);
$table->string('single_signon', 30)->nullable();
// Checks if mysql or mariadb supports json data type
if ((DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')) {
$table->json('settings')->nullable();
} else {
$table->text('settings')->nullable();
}
$table->softDeletes();
$table->rememberToken();
$table->timestamps();
$table->foreign('partner_id')->references('id')->on('partners')->onDelete('cascade');
});
}
public function down()
{
Schema::table('users', function(Blueprint $table) {
$table->dropForeign('users_partner_id_foreign');
});
Schema::drop('users');
}
最后我调用通知迁移:
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('notification_type', 10);
// Checks if mysql or mariadb supports json data type
if ((DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')) {
$table->json('notification')->nullable();
} else {
$table->text('notification')->nullable();
}
$table->boolean('seen');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::table('notifications', function(Blueprint $table) {
$table->dropForeign('notifications_user_id_foreign');
});
Schema::drop('notifications');
}
任何人都可以弄清楚这有什么问题吗?我正在使用 laravel 5.3 和 php7