Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Foreign Keys to "Grandparent": Duplicate Data?

Premium
4 min read

Let's talk about storing additional "duplicate" foreign keys, with an example from the open-source project Krayin CRM.


In the Krayin CRM project, there is a table product_inventories for which migration looks like this:

Schema::create('product_inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('in_stock')->default(0);
$table->integer('allocated')->default(0);
 
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
 
$table->integer('warehouse_id')->unsigned()->nullable();
$table->foreign('warehouse_id')->references('id')->on('warehouses')->onDelete('cascade');
 
$table->integer('warehouse_location_id')->unsigned()->nullable();
$table->foreign('warehouse_location_id')->references('id')->on('warehouse_locations')->onDelete('SET NULL');
 
$table->timestamps();
});

The warehouse_location_id field is from a grandparent table. A simplified schema looks like this:

So, we have the location_id field in both the "child" and "grandchild" tables, duplicating data.

Technically, this violates...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

S
Safy ✓ Link copied!

Is it posible to solve this problem using join query, store it in services, and call it by need?