1

My question might be confusing but I will edit it accordingly when I find better words. This is not a direct coding question, but more of an advice on how to approach this design problem.

In our DB model we store all products used in manufacturing (components, assemblies, material) and finished products for sale in the same table "products". Each product we sell and assemblies have their BOM saved in separate pivot table "product_boms". It's kind of like this:

Table: products
  - id
  - sku_number
  - name
  - type
  - ...

Table: product_boms
  - id
  - product_id (FK to products table)
  - child_product_id (FK to products table)
  - quantity

As you see table "product_boms" defines list of child products for certain product. This is part of entire BOM for this product. Part because a child product here can have further child products defined in the "product_boms" table.

I need to create a tree view of entire BOM for certain product. This is easy to do in a code, but results in many SQL queries as I need to look for every child product if it has further children... I am now looking into the SQL and if it's possible to get this out with a single query? I know this would be possible with stored procedure, but can't use those...

Final result (BOM) for product named "product_x" would look like this:

- product_x
   - component_a
   - component_b
   - assembly_a
      - material_a
      - component_c
      - component_d
   - assembly_b
      - material_b
      - component_e
      - component_f
      - component_g

I also have option to redefine DB model in order to save BOM more efficiently and better for getting it out of DB. Open to any suggestion.

Primoz Rome
  • 10,379
  • 17
  • 76
  • 108
  • Can a product ever have more than one parent product? i.e. could you ever find multiple rows in product_boms with the same child_product_id? – Jo Douglass Aug 20 '15 at 18:05
  • Yes of course, thanks for asking... There are assemblies and finished products that have same component for a child product... that is a common scenario. – Primoz Rome Aug 21 '15 at 06:47
  • 1
    Have a look here, at the top two answers: http://stackoverflow.com/questions/10646833/using-mysql-query-to-traverse-rows-to-make-a-recursive-tree – Jo Douglass Aug 22 '15 at 11:41

0 Answers0