KodaC's avatar
Level 1

->delete() deletes too much

My Migration

        Schema::create('temp_images', function (Blueprint $table) {
            $table->id();
            $table->string('image_hash');
            $table->string('image_path');
            $table->unsignedTinyInteger('is_scaled_image')->default(0);
            $table->timestamps();

            $table->unique(['image_hash', 'is_scaled_image'], 'temp_images_unque');
        });

My eloquent

$tempImage = TempImage::where('image_hash', $imageHash)
                ->where('is_scaled_image', $isScaledImage)
                ->firstOrFail();
            $tempImage->delete();

But now all entries are deleted with the same image_hash and not only the found entry. Spatie Ray confirms me that too

select
  *
from
  `temp_images`
where
  `image_hash` = '88640a365bc30fc070e8f300609f9b017f07e4dcda88c5151cf72626f995mkbo'
  and `is_scaled_image` = '0'
limit
  1

delete from
  `temp_images`
where
  `image_hash` = '88640a365bc30fc070e8f300609f9b017f07e4dcda88c5151cf72626f995mkbo'

What do I have to do that the deletion works correctly? Unfortunately I can not find a solution right now

0 likes
7 replies
LaryAI's avatar
Level 58

The issue is with the unique index on the image_hash and is_scaled_image columns. When you delete a row, it deletes all rows with the same image_hash value, regardless of the is_scaled_image value. To fix this, you can add a where clause to the delete statement to only delete the row with the specific id value:

$tempImage = TempImage::where('image_hash', $imageHash)
    ->where('is_scaled_image', $isScaledImage)
    ->firstOrFail();
$tempImage->where('id', $tempImage->id)->delete();
Snapey's avatar

Don't you have a unique primary key on the table?

KodaC's avatar
Level 1

@Snapey No. I It is only a table for a temporary storage i think which actually does not need a primary key.

Snapey's avatar

@KodaC your migration shows an id column ?

im trying to work out why eloquent is not deleting by id

What does the model look like?

KodaC's avatar
Level 1

@Snapey Sorry my mistake. There is one ID. I had a different table in my head. My model:

class TempImage extends Model
{
    use HasFactory;

    public $incrementing = false;
    public $primaryKey = 'image_hash';

    protected $fillable = [
        'image_hash',
        'image_path',
        'is_scaled_image',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'is_scaled_image' => 'boolean',
    ];
}

I see I still have incrementing and primaryKey in there from some old code ... stupid.... What would be the best solution in my case? Delete the ID, leave the rest and adjust the deletion like after the AI response?

newbie360's avatar

@KodaC In your migrtion there have an id column, and you say image_hash is NOT unique, so this not make sense

    public $incrementing = false;
    public $primaryKey = 'image_hash';

just remove it, let Eloquent handle the model by id

because this change, you need review the project code anywhere related to the model TempImage:: make sure all the logic is correct

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

@KodaC you need a unique primary key. If you don't.... well, you see the result

2 likes

Please or to participate in this conversation.