【问题标题】:Cannot resolve this error Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails无法解决此错误完整性约束违规:1451 无法删除或更新父行:外键约束失败
【发布时间】:2019-10-24 13:48:14
【问题描述】:

我想删除一个用户帐户当我使用 facebook 创建一个帐户并删除它显示此错误的 facebook 帐户时,它在简单帐户上工作正常。

完整性约束违规:1451 无法删除或更新父行:外键约束失败(mood_db.activities,CONSTRAINT activities_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id) )

我寻找解决此问题的方法,但找不到为什么无法删除 facebook 帐户。 这是我用于删除帐户的代码。

public function destroy($id)
{
    $user = User::find($id);

    $user->delete();

    return response()->json(['message' => 'User Deleted Succesfully'], 200);

}

这是活动的架构。

class CreateActivitiesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('activities', function (Blueprint $table) {
        $table->bigInteger('id', true)->unsigned();
        $table->text('activity_type');
        $table->bigInteger('user_id')->unsigned()->index('user_id');
        $table->bigInteger('category_id')->unsigned()->index('category_id');
        $table->bigInteger('subcategory_id')->unsigned()->index('subcategory_id');
        $table->text('activity_privacy');
        $table->integer('activity_privacy_visible')->default(0);
        $table->dateTime('activity_datetime_from');
        $table->dateTime('activity_datetime_to');
        $table->string('activity_address');
        $table->bigInteger('company_id')->unsigned()->nullable()->index('company_id');
        $table->decimal('latitude', 11, 8);
        $table->decimal('longitude', 11, 8);
        $table->integer('age_from');
        $table->integer('age_to');
        $table->string('people_limit')->nullable();
        $table->string('activity_picture')->nullable();
        $table->string('activity_title', 35)->nullable();
        $table->string('activity_description', 1000)->nullable();
        $table->timestamps();

        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        $table->foreign('category_id')
            ->references('id')
            ->on('categories')
            ->onDelete('cascade');

        $table->foreign('subcategory_id')
            ->references('id')
            ->on('subcategories')
            ->onDelete('cascade');

        $table->foreign('company_id')
            ->references('id')
            ->on('companies')
            ->onDelete('cascade');
    });
}

请帮助提前致谢。

【问题讨论】:

  • 我认为这个错误可以通过删除用户的活动记录然后删除用户记录来解决。
  • 你说我会先删除活动记录而不是删除用户?
  • 我使用 ondelete cascade 它会删除所有相关记录,但为什么会出现此错误? @VivekSolanki

标签: laravel-5


【解决方案1】:

请将代码设置为模型文件。

class User extends Eloquent
{
    public function activities() {
        return $this->has_many('Activity');
    }

    // this is a recommended way to declare event handlers
    public static function boot() {
        parent::boot();

        // before delete call this for child activity records delete
        static::deleting(function($user) { 
             $user->activities()->delete();             
        });
    }
}

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 2020-08-10
    • 1970-01-01
    • 2021-10-12
    • 2018-05-21
    • 1970-01-01
    • 2021-06-14
    • 2015-06-13
    • 2014-03-12
    相关资源
    最近更新 更多