I am developing a generic e-commerce solution using a relational database (MySQL), and I want to manage products like this:
- Each product (e.g. wood, plastic, steel) will have several attributes (e.g. thickness, width, height, color).
- In turn, each attribute can have several values (e.g. 3 mm or 5 mm for thickness, blue or red for color, etc.), which have a different price in each case.
So far so good, my current approach counts on 4 tables:
- products (id, name, desc)
- attributes (id, name, desc, type)
- attribute_values (id, attribute_id, value)
- attribute_product (attribute_id, product_id, price)
With this data model, I can combine products and attributes well, for example:
- Define a product called
wood
which have 2 attributes (thickness
andheight
) which in turn have3 mm
and5 mm
options for thickness andblue
andred
options for color...
The problem comes with price calculation because if I declare price in attribute_product pivot table, I still do not know which value is selected for that specific attribute and product.
How could I modify the data model to be able to somehow 'link' products with attribute values? Thanks in advance.