Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jabrij93's avatar

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "images" does not exist LINE 1: delete from "images" where "product_id" = $1 ^

When updating a form, I got an error saying, SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "images" does not exist LINE 1: delete from "images" where "product_id" = $1 ^ . And below it are the code. What does it mean and how can I fix this?

delete from "images" where "product_id" = 7
0 likes
1 reply
LaryAI's avatar
Level 58

The error message "SQLSTATE[42P01]: Undefined table: 7 ERROR: relation 'images' does not exist" indicates that the table 'images' does not exist in the database. This could be due to a migration not being run or a typo in the table name.

To fix this issue, you should check if the 'images' table exists in the database. If it does not exist, you should create it using a migration. If it does exist, you should check if the table name is spelled correctly in your code.

Example migration to create the 'images' table:

php artisan make:migration create_images_table --create=images

Then, add the following code to the migration file:

Schema::create('images', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('product_id');
    $table->string('image_path');
    $table->timestamps();

    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

Finally, run the migration using the following command:

php artisan migrate

Please or to participate in this conversation.