Question

I constructed the following toy example to illustrate my question.

The left side of the schema describes a an abstract product consisting of its parts.

The right side of the schema describes actual produced instances of these products/parts. The horizontal relations indicate from which "template" the instance was built. Note that it is possible that a part_instance is initially not yet assigned to a product_instance.

enter image description here

Here's the challenge:

Assigning a part_instance to a product_instance should only be possible if the part from which the part_instance was built is actually a part of the product from which product_instance was built from. So the (optional) hierarchy on the right should be enforced to correspond to the hierarchy on the left.

Or as an example: Let's assume part1 belongs to product1 and part2 belongs to product2. Now let's create productInstance1 linked to product1, productInstance2 linked to product2, partInstance1 linked to part1 and partInstance2 linked to part2. It should be allowed to assign partInstance1 to productInstance1, but not to productInstance2. Similarly it should be allowed to assign partInstance2 to productInstance2 but not to productInstance1.

Question:

Is it possible to enforce this rule in the database?

Was it helpful?

Solution

Based on the comments to my question it looks like the solution is to add product_id to the primary key of both the part and product_instance tables.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top