-- {{product.name}}: operational tables (PostgreSQL 17). -- -- One block per entity in the product's `entities` list, parents before children, association -- tables last. Names are snake_case of the MD-DDL names. Every table has: -- _id uuid the MD-DDL primary identifier; producers may supply it -- created_at / updated_at platform columns, always last -- Enum attributes are text with a CHECK listing the MD-DDL enum values verbatim. -- One-to-many relationships become a foreign key on the owned side; many-to-many -- relationships become association tables. Inheritance (`extends:`) is class-table: the -- subtype's primary key is its foreign key to the parent, and an abstract parent carries a --
_type discriminator naming the subtype. -- >>> repeat per core entity table (no `extends:`) CREATE TABLE {{product.schema}}.{{table}} ( {{table}}_id uuid PRIMARY KEY DEFAULT gen_random_uuid(), {{table}}_type text NOT NULL CHECK ({{table}}_type IN ({{subtype_names}})), -- abstract parents only {{column}} {{pg_type}}, -- {{attribute.name}} {{enum_column}} text CHECK ({{enum_column}} IN ({{enum_values}})), -- enum:{{enum.name}} {{parent_table}}_id uuid NOT NULL REFERENCES {{product.schema}}.{{parent_table}} ({{parent_table}}_id), -- one-to-many, owned side created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); COMMENT ON TABLE {{product.schema}}.{{table}} IS '{{entity.description}}'; COMMENT ON COLUMN {{product.schema}}.{{table}}.{{column}} IS '{{attribute.description}}'; CREATE INDEX {{table}}_{{parent_table}}_id_idx ON {{product.schema}}.{{table}} ({{parent_table}}_id); CREATE TRIGGER {{table}}_set_updated_at BEFORE UPDATE ON {{product.schema}}.{{table}} FOR EACH ROW EXECUTE FUNCTION {{product.schema}}.set_updated_at(); -- <<< end repeat -- >>> repeat per subtype table (`extends: {{parent}}`) CREATE TABLE {{product.schema}}.{{child_table}} ( {{child_table}}_id uuid PRIMARY KEY REFERENCES {{product.schema}}.{{parent_table}} ({{parent_table}}_id) ON DELETE CASCADE, {{column}} {{pg_type}}, -- {{attribute.name}} created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); COMMENT ON TABLE {{product.schema}}.{{child_table}} IS '{{entity.description}}'; CREATE TRIGGER {{child_table}}_set_updated_at BEFORE UPDATE ON {{product.schema}}.{{child_table}} FOR EACH ROW EXECUTE FUNCTION {{product.schema}}.set_updated_at(); -- <<< end repeat -- >>> repeat per many-to-many relationship -- Self-referential relationships are named
_association and use -- source_
_id / target_
_id in place of the two foreign keys below. CREATE TABLE {{product.schema}}.{{source_table}}_{{target_table}} ( {{source_table}}_{{target_table}}_id uuid PRIMARY KEY DEFAULT gen_random_uuid(), {{source_table}}_id uuid NOT NULL REFERENCES {{product.schema}}.{{source_table}} ({{source_table}}_id) ON DELETE CASCADE, {{target_table}}_id uuid NOT NULL REFERENCES {{product.schema}}.{{target_table}} ({{target_table}}_id) ON DELETE CASCADE, {{relationship_attribute}} {{pg_type}}, -- from relationship_attributes created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); COMMENT ON TABLE {{product.schema}}.{{source_table}}_{{target_table}} IS '{{relationship.description}}'; CREATE INDEX {{source_table}}_{{target_table}}_source_idx ON {{product.schema}}.{{source_table}}_{{target_table}} ({{source_table}}_id); CREATE INDEX {{source_table}}_{{target_table}}_target_idx ON {{product.schema}}.{{source_table}}_{{target_table}} ({{target_table}}_id); CREATE TRIGGER {{source_table}}_{{target_table}}_set_updated_at BEFORE UPDATE ON {{product.schema}}.{{source_table}}_{{target_table}} FOR EACH ROW EXECUTE FUNCTION {{product.schema}}.set_updated_at(); -- <<< end repeat