display field / delay creation of FKEY and function until the referenced table exists
authorMike Rylander <mrylander@gmail.com>
Thu, 19 Feb 2015 13:54:20 +0000 (08:54 -0500)
committerMike Rylander <mrylander@gmail.com>
Thu, 19 Feb 2015 13:54:20 +0000 (08:54 -0500)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/sql/Pg/002.schema.config.sql
Open-ILS/src/sql/Pg/800.fkeys.sql

index f8094cd..0fa1b8d 100644 (file)
@@ -169,16 +169,6 @@ INSERT INTO config.biblio_fingerprint (name, xpath, format, first_word)
         TRUE
     );
 
-CREATE OR REPLACE FUNCTION 
-    config.metabib_representative_field_is_valid(INTEGER, TEXT) RETURNS BOOLEAN AS $$
-    SELECT EXISTS (SELECT 1 FROM config.metabib_field WHERE id = $1 AND field_class = $2);
-$$ LANGUAGE SQL STRICT IMMUTABLE;
-
-COMMENT ON FUNCTION config.metabib_representative_field_is_valid(INTEGER, TEXT) IS $$
-Ensure the field_class value on the selected representative field matches
-the class name.
-$$;
-
 CREATE TABLE config.metabib_class (
     name     TEXT    PRIMARY KEY,
     label    TEXT    NOT NULL UNIQUE,
@@ -189,7 +179,7 @@ CREATE TABLE config.metabib_class (
     b_weight NUMERIC  DEFAULT 0.4 NOT NULL,
     c_weight NUMERIC  DEFAULT 0.2 NOT NULL,
     d_weight NUMERIC  DEFAULT 0.1 NOT NULL
-    representative_field INTEGER REFERENCES config.metabib_field(id),
+    representative_field INTEGER,
     CONSTRAINT rep_field_unique UNIQUE(representative_field),
     CONSTRAINT rep_field_is_valid CHECK (
         representative_field IS NULL OR
@@ -228,6 +218,17 @@ $$;
 
 CREATE UNIQUE INDEX config_metabib_field_class_name_idx ON config.metabib_field (field_class, name);
 
+CREATE OR REPLACE FUNCTION 
+    config.metabib_representative_field_is_valid(INTEGER, TEXT) RETURNS BOOLEAN AS $$
+    SELECT EXISTS (SELECT 1 FROM config.metabib_field WHERE id = $1 AND field_class = $2);
+$$ LANGUAGE SQL STRICT IMMUTABLE;
+
+COMMENT ON FUNCTION config.metabib_representative_field_is_valid(INTEGER, TEXT) IS $$
+Ensure the field_class value on the selected representative field matches
+the class name.
+$$;
+
+
 CREATE TABLE config.ts_config_list (
        id                      TEXT PRIMARY KEY,
        name            TEXT NOT NULL
index ee9886d..d092c80 100644 (file)
@@ -26,6 +26,9 @@ CREATE RULE protect_bib_rec_delete AS
             WHERE OLD.id = biblio.record_entry.id
     );
 
+ALTER TABLE config.metabib_class ADD CONSTRAINT config_metabib_class_representative_field_pkey FOREIGN KEY (representative_field)
+    REFERENCES config.metabib_field (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED;
+
 ALTER TABLE actor.usr ADD CONSTRAINT actor_usr_mailing_address_fkey FOREIGN KEY (mailing_address) REFERENCES actor.usr_address (id) DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE actor.usr ADD CONSTRAINT actor_usr_billing_address_fkey FOREIGN KEY (billing_address) REFERENCES actor.usr_address (id) DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE actor.usr ADD CONSTRAINT actor_usr_home_ou_fkey FOREIGN KEY (home_ou) REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED;