dimanche 9 juin 2019

Cannot resolve this error Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails

I wanted to delete a user account It works fine on simple account when i create a account with facebook and delete the facebook account it shows me this error.

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (mood_db.activities, CONSTRAINT activities_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id))

i search out to resolve this issue but could not find why can not delete account with facebook. Here is the code i use for delete account.

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

    $user->delete();

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

}

Here is the schema of activities.

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');
    });
}

Please help Thanks in Advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire