-- Product-level variation value selections (which category options apply to a product).
-- Store item variants are created later from these values.

CREATE TABLE product_variation_values (
  product_id    BIGINT NOT NULL,
  variation_id  BIGINT NOT NULL,
  option_id     BIGINT NOT NULL,
  created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),

  CONSTRAINT product_variation_values_pkey PRIMARY KEY (product_id, option_id),
  CONSTRAINT product_variation_values_product_id_fkey
    FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE,
  CONSTRAINT product_variation_values_variation_id_fkey
    FOREIGN KEY (variation_id) REFERENCES category_variations (id) ON DELETE RESTRICT,
  CONSTRAINT product_variation_values_option_id_fkey
    FOREIGN KEY (option_id) REFERENCES category_variation_options (id) ON DELETE RESTRICT
);

CREATE INDEX idx_product_variation_values_variation_id
  ON product_variation_values (variation_id);
