apskel-pos-backend/migrations/000028_create_ingredient_compositions_table.up.sql
2025-08-03 23:55:51 +07:00

21 lines
1.3 KiB
SQL

-- Ingredient compositions table
CREATE TABLE ingredient_compositions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
outlet_id UUID REFERENCES outlets(id) ON DELETE CASCADE,
parent_ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
child_ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
quantity DECIMAL(12,3) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes
CREATE INDEX idx_ingredient_compositions_organization_id ON ingredient_compositions(organization_id);
CREATE INDEX idx_ingredient_compositions_outlet_id ON ingredient_compositions(outlet_id);
CREATE INDEX idx_ingredient_compositions_parent_ingredient_id ON ingredient_compositions(parent_ingredient_id);
CREATE INDEX idx_ingredient_compositions_child_ingredient_id ON ingredient_compositions(child_ingredient_id);
CREATE INDEX idx_ingredient_compositions_created_at ON ingredient_compositions(created_at);
-- Unique constraint to prevent duplicate compositions
CREATE UNIQUE INDEX idx_ingredient_compositions_unique ON ingredient_compositions(parent_ingredient_id, child_ingredient_id);