andcl
andcl

Reputation: 3548

Data model for products with attributes, with different prices for each attributes combination

I am developing a generic e-commerce solution using a relational database (MySQL), and I want to manage products like this:

So far so good, my current approach counts on 4 tables:

With this data model, I can combine products and attributes well, for example:

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.

Upvotes: 1

Views: 1918

Answers (1)

Jon Wilson
Jon Wilson

Reputation: 776

Right, a linking table like attribute_product only link 1 instance to 1 instance, and your products can have multiple attributes, so that model doesn't work.

What you are calling the attribute_product is what most people would say is the product. It is the thing that has a price. So I would just call that "products". What you are calling products are perhaps product_categories.

So the attribute tables would be child tables to it.

  • product_categories (id, desc) -- eg. "Wood"
  • products (id, name, desc, category_id, price) -- eg. "Wood: 3x5mm Red"
  • attributes (id, name, desc, type) -- eg. "thickness"
  • product_attributes (id, product_id, attribute_id, value) -- multiple rows per product; eg. "3mm" "red"

Upvotes: 1

Related Questions