ajsmith_codes
5 months ago
614
4
Laravel

Help with database issue: Integrity constraint violation.

Posted 5 months ago by ajsmith_codes

I am building on the TDD Birdboard app with projects and tasks. Currently, the app records activity when a change is made to tasks and projects, as well as the creation of those. I edited it so that I can delete a task and that works.

My problem is that when I delete a project, I get an integrity constraint violation: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (laravel.activities, CONSTRAINT activities_project_id_foreign FOREIGN KEY (project_id) REFERENCES projects (id) ON DELETE CASCADE) (SQL: insert into activities (user_id, description, changes, project_id, updated_at, created_at) values (1, deleted_project, ?, 4, 2020-04-14 18:39:44, 2020-04-14 18:39:44))

Here is the code...

Trait RecordsActivity

public $oldAttributes = [];

/*
 * Boot the trait
 */
public static function bootRecordsActivity()
{

   foreach (self::recordableEvents() as $event) {
        static::$event(function ($model) use ($event) {
            $model->recordActivity($model->activityDescription($event));
        });


        if ($event === 'updated'){
            static::updating(function ($model) {
                $model->oldAttributes = $model->getOriginal();
            });
        }

   }

}

protected function activityDescription($description){

       return "{$description}_" . strtolower(class_basename($this)); //created_

}

/**
 * @return array
 */
protected static function recordableEvents(): array
{
    if (isset(static::$recordableEvents)) {
        return static::$recordableEvents;
    }
    return ['created', 'updated', 'deleted'];

}


/**
 * Record activity for a project.
 *
 * @param $description
 */
public function recordActivity($description)
{
    $this->activity()->create([
        'user_id' => ($this->project ?? $this)->owner->id,
        'description' => $description,
        'changes' => $this->activityChanges(),
        'project_id' => class_basename($this) === "Project" ? $this->id : $this->project_id
    ]);
}

protected function activityOwner(){

    if (auth()->check()){
        return auth()->user();
    }

    if (class_basename($this) === 'Project'){
        return $this->owner;
    }

    return $this->project->owner;

}


protected function activityChanges()
{
    if ($this->wasChanged()) {

        return [
            'before' => arr::except(array_diff($this->oldAttributes, $this->getAttributes()), 'updated_at'),
            'after' => arr::except($this->getChanges(), 'updated_at')
        ];

    }
}


/**
 * The activity feed for the project.
 *
 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
 */
public function activity()
{
    return $this->morphMany(Activity::class, 'subject')->latest();
}

This is what is called and is in the Task model:

/**
 * Model events that should trigger new activity.
 *
 * @var array
 */
protected static $recordableEvents = ['created', 'deleted', 'updated'];

Please sign in or create an account to participate in this conversation.