-- Order items table CREATE TABLE order_items ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), order_id UUID NOT NULL REFERENCES orders(id) ON DELETE CASCADE, product_id UUID NOT NULL REFERENCES products(id) ON DELETE RESTRICT, product_variant_id UUID REFERENCES product_variants(id) ON DELETE SET NULL, quantity INTEGER NOT NULL CHECK (quantity > 0), unit_price DECIMAL(10,2) NOT NULL CHECK (unit_price >= 0), total_price DECIMAL(10,2) NOT NULL CHECK (total_price >= 0), modifiers JSONB DEFAULT '[]', status VARCHAR(50) DEFAULT 'pending' CHECK (status IN ('pending', 'preparing', 'ready', 'served', 'cancelled')), created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); -- Indexes CREATE INDEX idx_order_items_order_id ON order_items(order_id); CREATE INDEX idx_order_items_product_id ON order_items(product_id); CREATE INDEX idx_order_items_product_variant_id ON order_items(product_variant_id); CREATE INDEX idx_order_items_status ON order_items(status); CREATE INDEX idx_order_items_created_at ON order_items(created_at);