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

shami003's avatar

Records not deleting with foreign key constraints

I want to delete product when the auction is deleted. I have used the foreign key constraints but the product is not deleting.

    public function up()
    {
        Schema::create('auctions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->timestamp('deadline');
            $table->timestamps();

            $table->foreign('product_id')
                ->references('id')
                ->on('products')
                ->onDelete('cascade');
        });
    }
0 likes
14 replies
tykus's avatar

Your relationship is backwards in that case; auctions is the child as currently designed.

1 like
shami003's avatar

@michaloravec I just change it to this way, I thought it will work. Actualy I am deleting product but my product_id is in auction table, I thought it is works this way :(

tykus's avatar

Cascading deletes work when the parent is deleted, not the child, you can delete the child without affecting the parent (on the database layer). You can handle it in the application layer if you do not wish to change where the foreign key is placed.

shami003's avatar

Now by deleting product I'm able to delete auction. But how to delete biddings of that auction in relation. It has not product_id.

public function up()
    {
        Schema::create('biddings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('auction_id');
            $table->integer('user_id');
            $table->integer('bidamount');
            $table->timestamps();
        });
    }

Using auction_id will work?

shami003's avatar

@tykus so the one with the foreign key is the parent of the other. For example in my case, auction is parent of product?

and for deleting parent we use foreign key in parent and delete child?

tykus's avatar
tykus
Best Answer
Level 104

Cascading deletes will cascade, so if Product cascades to Auction, Auction will cascade to Bidding (based on your current schema and a foreign key constraint on biddings which is not shown)

If you move the product-auction relationship FK onto the products table , then providing both products and biddings will have an auction_id FK, both will delete whenever an auction record is deleted.

shami003's avatar

@tykus As you said Product cascades to Auction, Auction will cascade to Bidding, if I delete the product. As I have created the schemas. Kindly check if it will work?

Also I am getting error of while running migrations

 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `auction`.`biddings` (errno: 150 "Foreign key constraint is incorrectly formed")")
Schema::create('biddings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('auction_id');
            $table->integer('user_id');
            $table->integer('bidamount');
            $table->timestamps();

            $table->foreign('auction_id')
                ->references('id')
                ->on('auctions')
                ->onDelete('cascade');
        });
Schema::create('auctions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->timestamp('deadline');
            $table->timestamps();

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

A foreign key might not be properly formed if the type on the column does not match the referenced column, or if the tables are being created out of order (e.g. auctions.product_id references products, but products migration is after auctions)

shami003's avatar

@tykus I changed it to

$table->bigIncrements('auction_id');

as reference id has that type but it didn't work as a table have only one auto column.

My products table is created before auctions. If that is incorrect how to change the sequence of creation? In my migrations list products comes before the auctions table

Please or to participate in this conversation.