23 lines
1.0 KiB
SQL
23 lines
1.0 KiB
SQL
CREATE TABLE product_reviews (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
order_item_id UUID NOT NULL REFERENCES order_items (id),
|
|
order_id UUID NOT NULL REFERENCES orders (id),
|
|
product_id UUID NOT NULL REFERENCES products (id),
|
|
shop_id UUID NOT NULL REFERENCES shops (id),
|
|
user_id UUID NOT NULL REFERENCES users (id),
|
|
rating INT NOT NULL CHECK (rating BETWEEN 1 AND 5),
|
|
content JSONB NOT NULL,
|
|
images JSONB NOT NULL DEFAULT '[]',
|
|
reply JSONB,
|
|
reply_at TIMESTAMPTZ,
|
|
status TEXT NOT NULL DEFAULT 'visible' CHECK (status IN ('visible', 'hidden')),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- At most one review per order line, forever.
|
|
CREATE UNIQUE INDEX product_reviews_one_per_line ON product_reviews (order_item_id);
|
|
CREATE INDEX idx_product_reviews_product ON product_reviews (product_id) WHERE status = 'visible';
|
|
CREATE INDEX idx_product_reviews_shop ON product_reviews (shop_id);
|
|
CREATE INDEX idx_product_reviews_user ON product_reviews (user_id);
|