-- Generated by CompileSchemaScripts.pl from:
-- 20140308-ordering-columns-fk.sql
-- 20140312-dates-trigger.sql
-- 20140217-instrument-triggers.sql
-- 20140318-series-fks.sql
\set ON_ERROR_STOP 1
BEGIN;
--------------------------------------------------------------------------------
SELECT '20140308-ordering-columns-fk.sql';


ALTER TABLE area_alias_type
   ADD CONSTRAINT area_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES area_alias_type(id);

ALTER TABLE area_type
   ADD CONSTRAINT area_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES area_type(id);

ALTER TABLE artist_type
   ADD CONSTRAINT artist_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES artist_type(id);

ALTER TABLE artist_alias_type
   ADD CONSTRAINT artist_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES artist_alias_type(id);

ALTER TABLE gender
   ADD CONSTRAINT gender_fk_parent
   FOREIGN KEY (parent)
   REFERENCES gender(id);

ALTER TABLE label_type
   ADD CONSTRAINT label_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES label_type(id);

ALTER TABLE label_alias_type
   ADD CONSTRAINT label_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES label_alias_type(id);

ALTER TABLE place_type
   ADD CONSTRAINT place_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES place_type(id);

ALTER TABLE place_alias_type
   ADD CONSTRAINT place_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES place_alias_type(id);

ALTER TABLE release_group_primary_type
   ADD CONSTRAINT release_group_primary_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES release_group_primary_type(id);

ALTER TABLE release_group_secondary_type
   ADD CONSTRAINT release_group_secondary_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES release_group_secondary_type(id);

ALTER TABLE release_packaging
   ADD CONSTRAINT release_packaging_fk_parent
   FOREIGN KEY (parent)
   REFERENCES release_packaging(id);

ALTER TABLE release_status
   ADD CONSTRAINT release_status_fk_parent
   FOREIGN KEY (parent)
   REFERENCES release_status(id);

ALTER TABLE work_alias_type
   ADD CONSTRAINT work_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES work_alias_type(id);

ALTER TABLE work_attribute_type
   ADD CONSTRAINT work_attribute_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES work_attribute_type(id);

ALTER TABLE work_attribute_type_allowed_value
   ADD CONSTRAINT work_attribute_type_allowed_value_fk_parent
   FOREIGN KEY (parent)
   REFERENCES work_attribute_type_allowed_value(id);

ALTER TABLE work_type
   ADD CONSTRAINT work_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES work_type(id);

ALTER TABLE cover_art_archive.art_type
   ADD CONSTRAINT art_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES cover_art_archive.art_type(id);

--------------------------------------------------------------------------------
SELECT '20140312-dates-trigger.sql';

CREATE OR REPLACE FUNCTION check_has_dates()
RETURNS trigger AS $$
BEGIN
    IF (NEW.begin_date_year IS NOT NULL OR
       NEW.begin_date_month IS NOT NULL OR
       NEW.begin_date_day IS NOT NULL OR
       NEW.end_date_year IS NOT NULL OR
       NEW.end_date_month IS NOT NULL OR
       NEW.end_date_day IS NOT NULL OR
       NEW.ended = TRUE)
       AND NOT (SELECT has_dates FROM link_type WHERE id = NEW.link_type)
  THEN
    RAISE EXCEPTION 'Attempt to add dates to a relationship type that does not support dates.';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';

CREATE TRIGGER check_has_dates BEFORE UPDATE OR INSERT ON link
    FOR EACH ROW EXECUTE PROCEDURE check_has_dates();

--------------------------------------------------------------------------------
SELECT '20140217-instrument-triggers.sql';


CREATE OR REPLACE FUNCTION a_ins_instrument() RETURNS TRIGGER AS $$
BEGIN
    INSERT INTO link_attribute_type (parent, root, child_order, gid, name, description) VALUES (14, 14, 0, NEW.gid, NEW.name, NEW.description);

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION a_upd_instrument() RETURNS trigger AS $$
BEGIN
    UPDATE link_attribute_type SET name = NEW.name, description = NEW.description WHERE gid = NEW.gid;
    IF NOT FOUND THEN
        RAISE EXCEPTION 'no link_attribute_type found for instrument %', NEW.gid;
    ELSE
        RETURN NEW;
    END IF;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION a_del_instrument() RETURNS trigger AS $$
BEGIN
    DELETE FROM link_attribute_type WHERE gid = OLD.gid;
    IF NOT FOUND THEN
        RAISE EXCEPTION 'no link_attribute_type found for instrument %', NEW.gid;
    ELSE
        RETURN NEW;
    END IF;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION unique_primary_instrument_alias()
RETURNS trigger AS $$
BEGIN
    IF NEW.primary_for_locale THEN
      UPDATE instrument_alias SET primary_for_locale = FALSE
      WHERE locale = NEW.locale AND id != NEW.id
        AND instrument = NEW.instrument;
    END IF;
    RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';


ALTER TABLE instrument ADD CHECK (controlled_for_whitespace(comment));

ALTER TABLE instrument
  ADD CONSTRAINT control_for_whitespace CHECK (controlled_for_whitespace(name)),
  ADD CONSTRAINT only_non_empty CHECK (name != '');

ALTER TABLE instrument_alias
  ADD CONSTRAINT control_for_whitespace CHECK (controlled_for_whitespace(name)),
  ADD CONSTRAINT only_non_empty CHECK (name != ''),
  ADD CONSTRAINT control_for_whitespace_sort_name CHECK (controlled_for_whitespace(sort_name)),
  ADD CONSTRAINT only_non_empty_sort_name CHECK (sort_name != '');

ALTER TABLE edit_instrument
   ADD CONSTRAINT edit_instrument_fk_edit
   FOREIGN KEY (edit)
   REFERENCES edit(id);

ALTER TABLE edit_instrument
   ADD CONSTRAINT edit_instrument_fk_instrument
   FOREIGN KEY (instrument)
   REFERENCES instrument(id)
   ON DELETE CASCADE;

ALTER TABLE instrument
   ADD CONSTRAINT instrument_fk_type
   FOREIGN KEY (type)
   REFERENCES instrument_type(id);

ALTER TABLE instrument_alias
   ADD CONSTRAINT instrument_alias_fk_instrument
   FOREIGN KEY (instrument)
   REFERENCES instrument(id);

ALTER TABLE instrument_alias
   ADD CONSTRAINT instrument_alias_fk_type
   FOREIGN KEY (type)
   REFERENCES instrument_alias_type(id);

ALTER TABLE instrument_annotation
   ADD CONSTRAINT instrument_annotation_fk_instrument
   FOREIGN KEY (instrument)
   REFERENCES instrument(id);

ALTER TABLE instrument_annotation
   ADD CONSTRAINT instrument_annotation_fk_annotation
   FOREIGN KEY (annotation)
   REFERENCES annotation(id);

ALTER TABLE instrument_gid_redirect
   ADD CONSTRAINT instrument_gid_redirect_fk_new_id
   FOREIGN KEY (new_id)
   REFERENCES instrument(id);

ALTER TABLE l_area_instrument
   ADD CONSTRAINT l_area_instrument_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_area_instrument
   ADD CONSTRAINT l_area_instrument_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES area(id);

ALTER TABLE l_area_instrument
   ADD CONSTRAINT l_area_instrument_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES instrument(id);

ALTER TABLE l_artist_instrument
   ADD CONSTRAINT l_artist_instrument_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_artist_instrument
   ADD CONSTRAINT l_artist_instrument_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES artist(id);

ALTER TABLE l_artist_instrument
   ADD CONSTRAINT l_artist_instrument_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_instrument
   ADD CONSTRAINT l_instrument_instrument_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_instrument
   ADD CONSTRAINT l_instrument_instrument_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_instrument
   ADD CONSTRAINT l_instrument_instrument_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_label
   ADD CONSTRAINT l_instrument_label_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_label
   ADD CONSTRAINT l_instrument_label_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_label
   ADD CONSTRAINT l_instrument_label_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES label(id);

ALTER TABLE l_instrument_place
   ADD CONSTRAINT l_instrument_place_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_place
   ADD CONSTRAINT l_instrument_place_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_place
   ADD CONSTRAINT l_instrument_place_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES place(id);

ALTER TABLE l_instrument_recording
   ADD CONSTRAINT l_instrument_recording_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_recording
   ADD CONSTRAINT l_instrument_recording_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_recording
   ADD CONSTRAINT l_instrument_recording_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES recording(id);

ALTER TABLE l_instrument_release
   ADD CONSTRAINT l_instrument_release_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_release
   ADD CONSTRAINT l_instrument_release_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_release
   ADD CONSTRAINT l_instrument_release_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES release(id);

ALTER TABLE l_instrument_release_group
   ADD CONSTRAINT l_instrument_release_group_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_release_group
   ADD CONSTRAINT l_instrument_release_group_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_release_group
   ADD CONSTRAINT l_instrument_release_group_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES release_group(id);

ALTER TABLE l_instrument_url
   ADD CONSTRAINT l_instrument_url_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_url
   ADD CONSTRAINT l_instrument_url_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_url
   ADD CONSTRAINT l_instrument_url_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES url(id);

ALTER TABLE l_instrument_work
   ADD CONSTRAINT l_instrument_work_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_work
   ADD CONSTRAINT l_instrument_work_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_work
   ADD CONSTRAINT l_instrument_work_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES work(id);


CREATE TRIGGER b_upd_instrument BEFORE UPDATE ON instrument
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER end_date_implies_ended BEFORE UPDATE OR INSERT ON instrument_alias
    FOR EACH ROW EXECUTE PROCEDURE end_date_implies_ended();

CREATE TRIGGER b_upd_instrument_alias BEFORE UPDATE ON instrument_alias
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER unique_primary_for_locale BEFORE UPDATE OR INSERT ON instrument_alias
    FOR EACH ROW EXECUTE PROCEDURE unique_primary_instrument_alias();

CREATE TRIGGER search_hint BEFORE UPDATE OR INSERT ON instrument_alias
    FOR EACH ROW EXECUTE PROCEDURE simplify_search_hints(2);
CREATE TRIGGER b_upd_l_area_instrument BEFORE UPDATE ON l_area_instrument
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_artist_instrument BEFORE UPDATE ON l_artist_instrument
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_instrument BEFORE UPDATE ON l_instrument_instrument
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_label BEFORE UPDATE ON l_instrument_label
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_place BEFORE UPDATE ON l_instrument_place
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_recording BEFORE UPDATE ON l_instrument_recording
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_release BEFORE UPDATE ON l_instrument_release
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_release_group BEFORE UPDATE ON l_instrument_release_group
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_url BEFORE UPDATE ON l_instrument_url
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_work BEFORE UPDATE ON l_instrument_work
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER a_ins_instrument AFTER INSERT ON musicbrainz.instrument
    FOR EACH ROW EXECUTE PROCEDURE a_ins_instrument();

CREATE TRIGGER a_upd_instrument AFTER UPDATE ON musicbrainz.instrument
    FOR EACH ROW EXECUTE PROCEDURE a_upd_instrument();

CREATE TRIGGER a_del_instrument AFTER DELETE ON musicbrainz.instrument
    FOR EACH ROW EXECUTE PROCEDURE a_del_instrument();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_area_instrument DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();
CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_artist_instrument DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

 CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_instrument DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_label DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_place DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_recording DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_release DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_release_group DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_url DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_work DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER url_gc_a_upd_l_instrument_url
    AFTER UPDATE ON l_instrument_url DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_url();

CREATE CONSTRAINT TRIGGER url_gc_a_del_l_instrument_url
    AFTER DELETE ON l_instrument_url DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_url();

ALTER TABLE documentation.l_area_instrument_example
   ADD CONSTRAINT l_area_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_instrument(id);

ALTER TABLE documentation.l_artist_instrument_example
   ADD CONSTRAINT l_artist_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_instrument(id);

ALTER TABLE documentation.l_instrument_instrument_example
   ADD CONSTRAINT l_instrument_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_instrument(id);

ALTER TABLE documentation.l_instrument_label_example
   ADD CONSTRAINT l_instrument_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_label(id);

ALTER TABLE documentation.l_instrument_place_example
   ADD CONSTRAINT l_instrument_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_place(id);

ALTER TABLE documentation.l_instrument_recording_example
   ADD CONSTRAINT l_instrument_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_recording(id);

ALTER TABLE documentation.l_instrument_release_example
   ADD CONSTRAINT l_instrument_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_release(id);

ALTER TABLE documentation.l_instrument_release_group_example
   ADD CONSTRAINT l_instrument_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_release_group(id);

ALTER TABLE documentation.l_instrument_url_example
   ADD CONSTRAINT l_instrument_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_url(id);

ALTER TABLE documentation.l_instrument_work_example
   ADD CONSTRAINT l_instrument_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_work(id);

--------------------------------------------------------------------------------
SELECT '20140318-series-fks.sql';

------------------
-- constraints  --
------------------

ALTER TABLE series ADD CHECK (controlled_for_whitespace(comment));

ALTER TABLE link_attribute_text_value
  ADD CONSTRAINT control_for_whitespace CHECK (controlled_for_whitespace(text_value)),
  ADD CONSTRAINT only_non_empty CHECK (text_value != '');

ALTER TABLE series
  ADD CONSTRAINT control_for_whitespace CHECK (controlled_for_whitespace(name)),
  ADD CONSTRAINT only_non_empty CHECK (name != '');

ALTER TABLE series_type ADD CONSTRAINT allowed_series_entity_type
  CHECK (
    entity_type IN (
      'recording',
      'release',
      'release_group',
      'work'
    )
  );

------------------
-- foreign keys --
------------------

ALTER TABLE documentation.l_area_series_example ADD CONSTRAINT l_area_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_area_series(id);
ALTER TABLE documentation.l_artist_series_example ADD CONSTRAINT l_artist_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_artist_series(id);
ALTER TABLE documentation.l_instrument_series_example ADD CONSTRAINT l_instrument_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_instrument_series(id);
ALTER TABLE documentation.l_label_series_example ADD CONSTRAINT l_label_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_label_series(id);
ALTER TABLE documentation.l_place_series_example ADD CONSTRAINT l_place_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_place_series(id);
ALTER TABLE documentation.l_recording_series_example ADD CONSTRAINT l_recording_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_recording_series(id);
ALTER TABLE documentation.l_release_group_series_example ADD CONSTRAINT l_release_group_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_release_group_series(id);
ALTER TABLE documentation.l_release_series_example ADD CONSTRAINT l_release_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_release_series(id);
ALTER TABLE documentation.l_series_series_example ADD CONSTRAINT l_series_series_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_series_series(id);
ALTER TABLE documentation.l_series_url_example ADD CONSTRAINT l_series_url_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_series_url(id);
ALTER TABLE documentation.l_series_work_example ADD CONSTRAINT l_series_work_example_fk_id FOREIGN KEY (id) REFERENCES musicbrainz.l_series_work(id);

ALTER TABLE editor_subscribe_series ADD CONSTRAINT editor_subscribe_series_fk_editor FOREIGN KEY (editor) REFERENCES editor(id);
ALTER TABLE editor_subscribe_series ADD CONSTRAINT editor_subscribe_series_fk_series FOREIGN KEY (series) REFERENCES series(id);
ALTER TABLE editor_subscribe_series ADD CONSTRAINT editor_subscribe_series_fk_last_edit_sent FOREIGN KEY (last_edit_sent) REFERENCES edit(id);

ALTER TABLE editor_subscribe_series_deleted ADD CONSTRAINT editor_subscribe_series_deleted_fk_editor FOREIGN KEY (editor) REFERENCES editor(id);
ALTER TABLE editor_subscribe_series_deleted ADD CONSTRAINT editor_subscribe_series_deleted_fk_gid FOREIGN KEY (gid) REFERENCES series_deletion(gid);
ALTER TABLE editor_subscribe_series_deleted ADD CONSTRAINT editor_subscribe_series_deleted_fk_deleted_by FOREIGN KEY (deleted_by) REFERENCES edit(id);

ALTER TABLE l_area_series ADD CONSTRAINT l_area_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_area_series ADD CONSTRAINT l_area_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES area(id);
ALTER TABLE l_area_series ADD CONSTRAINT l_area_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_artist_series ADD CONSTRAINT l_artist_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_artist_series ADD CONSTRAINT l_artist_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES artist(id);
ALTER TABLE l_artist_series ADD CONSTRAINT l_artist_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_instrument_series ADD CONSTRAINT l_instrument_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_instrument_series ADD CONSTRAINT l_instrument_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES instrument(id);
ALTER TABLE l_instrument_series ADD CONSTRAINT l_instrument_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_label_series ADD CONSTRAINT l_label_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_label_series ADD CONSTRAINT l_label_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES label(id);
ALTER TABLE l_label_series ADD CONSTRAINT l_label_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_place_series ADD CONSTRAINT l_place_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_place_series ADD CONSTRAINT l_place_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES place(id);
ALTER TABLE l_place_series ADD CONSTRAINT l_place_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_recording_series ADD CONSTRAINT l_recording_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_recording_series ADD CONSTRAINT l_recording_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES recording(id);
ALTER TABLE l_recording_series ADD CONSTRAINT l_recording_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_release_group_series ADD CONSTRAINT l_release_group_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_release_group_series ADD CONSTRAINT l_release_group_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES release_group(id);
ALTER TABLE l_release_group_series ADD CONSTRAINT l_release_group_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_release_series ADD CONSTRAINT l_release_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_release_series ADD CONSTRAINT l_release_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES release(id);
ALTER TABLE l_release_series ADD CONSTRAINT l_release_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_series_series ADD CONSTRAINT l_series_series_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_series_series ADD CONSTRAINT l_series_series_fk_entity0 FOREIGN KEY (entity0) REFERENCES series(id);
ALTER TABLE l_series_series ADD CONSTRAINT l_series_series_fk_entity1 FOREIGN KEY (entity1) REFERENCES series(id);

ALTER TABLE l_series_url ADD CONSTRAINT l_series_url_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_series_url ADD CONSTRAINT l_series_url_fk_entity0 FOREIGN KEY (entity0) REFERENCES series(id);
ALTER TABLE l_series_url ADD CONSTRAINT l_series_url_fk_entity1 FOREIGN KEY (entity1) REFERENCES url(id);

ALTER TABLE l_series_work ADD CONSTRAINT l_series_work_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE l_series_work ADD CONSTRAINT l_series_work_fk_entity0 FOREIGN KEY (entity0) REFERENCES series(id);
ALTER TABLE l_series_work ADD CONSTRAINT l_series_work_fk_entity1 FOREIGN KEY (entity1) REFERENCES work(id);

ALTER TABLE link_attribute_text_value ADD CONSTRAINT link_attribute_text_value_fk_link FOREIGN KEY (link) REFERENCES link(id);
ALTER TABLE link_attribute_text_value ADD CONSTRAINT link_attribute_text_value_fk_attribute_type FOREIGN KEY (attribute_type) REFERENCES link_text_attribute_type(attribute_type);
ALTER TABLE link_text_attribute_type ADD CONSTRAINT link_text_attribute_type_fk_attribute_type FOREIGN KEY (attribute_type) REFERENCES link_attribute_type(id) ON DELETE CASCADE;

ALTER TABLE orderable_link_type ADD CONSTRAINT orderable_link_type_fk_link_type FOREIGN KEY (link_type) REFERENCES link_type(id);

ALTER TABLE series ADD CONSTRAINT series_fk_type FOREIGN KEY (type) REFERENCES series_type(id);
ALTER TABLE series ADD CONSTRAINT series_fk_ordering_attribute FOREIGN KEY (ordering_attribute) REFERENCES link_text_attribute_type(attribute_type);
ALTER TABLE series ADD CONSTRAINT series_fk_ordering_type FOREIGN KEY (ordering_type) REFERENCES series_ordering_type(id);

ALTER TABLE series_alias ADD CONSTRAINT series_alias_fk_series FOREIGN KEY (series) REFERENCES series(id);
ALTER TABLE series_alias ADD CONSTRAINT series_alias_fk_type FOREIGN KEY (type) REFERENCES series_alias_type(id);

ALTER TABLE series_annotation ADD CONSTRAINT series_annotation_fk_series FOREIGN KEY (series) REFERENCES series(id);
ALTER TABLE series_annotation ADD CONSTRAINT series_annotation_fk_annotation FOREIGN KEY (annotation) REFERENCES annotation(id);

ALTER TABLE series_gid_redirect ADD CONSTRAINT series_gid_redirect_fk_new_id FOREIGN KEY (new_id) REFERENCES series(id);

ALTER TABLE series_ordering_type ADD CONSTRAINT series_ordering_type_fk_parent FOREIGN KEY (parent) REFERENCES series_ordering_type(id);

ALTER TABLE series_type ADD CONSTRAINT series_type_fk_parent FOREIGN KEY (parent) REFERENCES series_type(id);

COMMIT;
