/* Prevent "cannot alter type of a column used by a view or rule" errors when
cleanup operations are run using in place UPDATE */
DROP VIEW IF EXISTS view_all_commnames_code CASCADE;
DROP VIEW IF EXISTS view_all_commnames_common CASCADE;
DROP VIEW IF EXISTS view_all_commnames_sciname CASCADE;
DROP VIEW IF EXISTS view_all_commnames_translated CASCADE;
DROP VIEW IF EXISTS view_all_plantnames_code CASCADE;
DROP VIEW IF EXISTS view_all_plantnames_common CASCADE;
DROP VIEW IF EXISTS view_all_plantnames_sciname CASCADE;
DROP VIEW IF EXISTS view_all_plantnames_scinamenoauth CASCADE;
DROP VIEW IF EXISTS view_notemb_observation CASCADE;
DROP VIEW IF EXISTS view_browseparty_classcontrib CASCADE;
DROP VIEW IF EXISTS view_browseparty_obscontrib CASCADE;
DROP VIEW IF EXISTS view_browseparty_projectcontrib CASCADE;
DROP VIEW IF EXISTS view_browseparty_all CASCADE;
DROP VIEW IF EXISTS view_browseparty_all_count CASCADE;
DROP VIEW IF EXISTS view_browseparty_classcontrib_count CASCADE;
DROP VIEW IF EXISTS view_browseparty_obscontrib_count CASCADE;
DROP VIEW IF EXISTS view_browseparty_projectcontrib_count CASCADE;
DROP VIEW IF EXISTS view_browseparty_all_count_combined CASCADE;
DROP VIEW IF EXISTS view_busrule_duplcovercode CASCADE;
DROP VIEW IF EXISTS view_busrule_duplstratumtype CASCADE;
DROP VIEW IF EXISTS view_busrule_plotsizeshape CASCADE;
DROP VIEW IF EXISTS view_reference_transl CASCADE;
DROP VIEW IF EXISTS view_commconcept_transl CASCADE;
DROP VIEW IF EXISTS view_notemb_commclass CASCADE;
DROP VIEW IF EXISTS view_comminterp_more CASCADE;
DROP VIEW IF EXISTS view_csv_taxonimportance_pre CASCADE;
DROP VIEW IF EXISTS view_csv_taxonimportance CASCADE;
DROP VIEW IF EXISTS view_dbafielddesc_notimpl CASCADE;
DROP VIEW IF EXISTS view_emb_embargo_currentfullonly CASCADE;
DROP VIEW IF EXISTS view_emb_embargo_complete CASCADE;
DROP VIEW IF EXISTS view_keywprojplaces CASCADE;
DROP VIEW IF EXISTS view_kwhelper_projcontrib CASCADE;
DROP VIEW IF EXISTS view_kwhelper_refjournal CASCADE;
DROP VIEW IF EXISTS view_kwhelper_refparty CASCADE;
DROP VIEW IF EXISTS view_notemb_classcontributor CASCADE;
DROP VIEW IF EXISTS view_notemb_comminterpretation CASCADE;
DROP VIEW IF EXISTS view_notemb_disturbanceobs CASCADE;
DROP VIEW IF EXISTS view_notemb_plot CASCADE;
DROP VIEW IF EXISTS view_notemb_soilobs CASCADE;
DROP VIEW IF EXISTS view_notemb_stemcount CASCADE;
DROP VIEW IF EXISTS view_notemb_stemlocation CASCADE;
DROP VIEW IF EXISTS view_notemb_taxonalt CASCADE;
DROP VIEW IF EXISTS view_notemb_taxonimportance CASCADE;
DROP VIEW IF EXISTS view_notemb_taxoninterpretation CASCADE;
DROP VIEW IF EXISTS view_notemb_taxonobservation CASCADE;
DROP VIEW IF EXISTS view_observation_transl CASCADE;
DROP VIEW IF EXISTS view_party_public CASCADE;
DROP VIEW IF EXISTS view_party_transl CASCADE;
DROP VIEW IF EXISTS view_plantconcept_transl CASCADE;
DROP VIEW IF EXISTS view_plotall_withembargo CASCADE;
DROP VIEW IF EXISTS view_std_commnames_code CASCADE;
DROP VIEW IF EXISTS view_std_commnames_common CASCADE;
DROP VIEW IF EXISTS view_std_commnames_sciname CASCADE;
DROP VIEW IF EXISTS view_std_commnames_translated CASCADE;
DROP VIEW IF EXISTS view_std_plantnames_code CASCADE;
DROP VIEW IF EXISTS view_std_plantnames_common CASCADE;
DROP VIEW IF EXISTS view_std_plantnames_sciname CASCADE;
DROP VIEW IF EXISTS view_std_plantnames_scinamenoauth CASCADE;
DROP VIEW IF EXISTS view_taxoninterp_more CASCADE;
DROP VIEW IF EXISTS view_taxonobs_withmaxcover CASCADE;
DROP VIEW IF EXISTS view_taxonobs_distinctid_curr CASCADE;
DROP VIEW IF EXISTS view_taxonobs_distinctid_curr_counts CASCADE;
DROP VIEW IF EXISTS view_taxonobs_distinctid_curr_counts_plants CASCADE;

-- Enable cascading deletes

ALTER TABLE address DROP CONSTRAINT r1address_party_id;
ALTER TABLE ONLY address ADD CONSTRAINT r1address_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE classcontributor DROP CONSTRAINT r1classcontributor_commclass_id;
ALTER TABLE ONLY classcontributor ADD CONSTRAINT r1classcontributor_commclass_id FOREIGN KEY (commclass_id) REFERENCES commclass(commclass_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commclass DROP CONSTRAINT r1commclass_observation_id;
ALTER TABLE ONLY commclass ADD CONSTRAINT r1commclass_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commconcept DROP CONSTRAINT r1commconcept_commname_id;
ALTER TABLE ONLY commconcept ADD CONSTRAINT r1commconcept_commname_id FOREIGN KEY (commname_id) REFERENCES commname(commname_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commcorrelation DROP CONSTRAINT r1commcorrelation_commstatus_id;
ALTER TABLE ONLY commcorrelation ADD CONSTRAINT r1commcorrelation_commstatus_id FOREIGN KEY (commstatus_id) REFERENCES commstatus(commstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE comminterpretation DROP CONSTRAINT r1comminterpretation_commclass_id;
ALTER TABLE ONLY comminterpretation ADD CONSTRAINT r1comminterpretation_commclass_id FOREIGN KEY (commclass_id) REFERENCES commclass(commclass_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commlineage DROP CONSTRAINT r1commlineage_parentcommstatus_id;
ALTER TABLE ONLY commlineage ADD CONSTRAINT r1commlineage_parentcommstatus_id FOREIGN KEY (parentcommstatus_id) REFERENCES commstatus(commstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commname DROP CONSTRAINT r1commname_reference_id;
ALTER TABLE ONLY commname ADD CONSTRAINT r1commname_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commstatus DROP CONSTRAINT r1commstatus_commconcept_id;
ALTER TABLE ONLY commstatus ADD CONSTRAINT r1commstatus_commconcept_id FOREIGN KEY (commconcept_id) REFERENCES commconcept(commconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commusage DROP CONSTRAINT r1commusage_commname_id;
ALTER TABLE ONLY commusage ADD CONSTRAINT r1commusage_commname_id FOREIGN KEY (commname_id) REFERENCES commname(commname_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE coverindex DROP CONSTRAINT r1coverindex_covermethod_id;
ALTER TABLE ONLY coverindex ADD CONSTRAINT r1coverindex_covermethod_id FOREIGN KEY (covermethod_id) REFERENCES covermethod(covermethod_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE covermethod DROP CONSTRAINT r1covermethod_reference_id;
ALTER TABLE ONLY covermethod ADD CONSTRAINT r1covermethod_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE definedvalue DROP CONSTRAINT r1definedvalue_userdefined_id;
ALTER TABLE ONLY definedvalue ADD CONSTRAINT r1definedvalue_userdefined_id FOREIGN KEY (userdefined_id) REFERENCES userdefined(userdefined_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE disturbanceobs DROP CONSTRAINT r1disturbanceobs_observation_id;
ALTER TABLE ONLY disturbanceobs ADD CONSTRAINT r1disturbanceobs_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE embargo DROP CONSTRAINT r1embargo_plot_id;
ALTER TABLE ONLY embargo ADD CONSTRAINT r1embargo_plot_id FOREIGN KEY (plot_id) REFERENCES plot(plot_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE graphic DROP CONSTRAINT r1graphic_observation_id;
ALTER TABLE ONLY graphic ADD CONSTRAINT r1graphic_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE namedplace DROP CONSTRAINT r1namedplace_reference_id;
ALTER TABLE ONLY namedplace ADD CONSTRAINT r1namedplace_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE namedplacecorrelation DROP CONSTRAINT r1namedplacecorrelation_parentplace_id;
ALTER TABLE ONLY namedplacecorrelation ADD CONSTRAINT r1namedplacecorrelation_parentplace_id FOREIGN KEY (parentplace_id) REFERENCES namedplace(namedplace_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE note DROP CONSTRAINT r1note_notelink_id;
ALTER TABLE ONLY note ADD CONSTRAINT r1note_notelink_id FOREIGN KEY (notelink_id) REFERENCES notelink(notelink_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observation DROP CONSTRAINT r1observation_previousobs_id;
ALTER TABLE ONLY observation ADD CONSTRAINT r1observation_previousobs_id FOREIGN KEY (previousobs_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationcontributor DROP CONSTRAINT r1observationcontributor_observation_id;
ALTER TABLE ONLY observationcontributor ADD CONSTRAINT r1observationcontributor_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationsynonym DROP CONSTRAINT r1observationsynonym_synonymobservation_id;
ALTER TABLE ONLY observationsynonym ADD CONSTRAINT r1observationsynonym_synonymobservation_id FOREIGN KEY (synonymobservation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE party DROP CONSTRAINT r1party_currentname_id;
ALTER TABLE ONLY party ADD CONSTRAINT r1party_currentname_id FOREIGN KEY (currentname_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE partymember DROP CONSTRAINT r1partymember_parentparty_id;
ALTER TABLE ONLY partymember ADD CONSTRAINT r1partymember_parentparty_id FOREIGN KEY (parentparty_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE place DROP CONSTRAINT r1place_plot_id;
ALTER TABLE ONLY place ADD CONSTRAINT r1place_plot_id FOREIGN KEY (plot_id) REFERENCES plot(plot_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantconcept DROP CONSTRAINT r1plantconcept_plantname_id;
ALTER TABLE ONLY plantconcept ADD CONSTRAINT r1plantconcept_plantname_id FOREIGN KEY (plantname_id) REFERENCES plantname(plantname_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantcorrelation DROP CONSTRAINT r1plantcorrelation_plantstatus_id;
ALTER TABLE ONLY plantcorrelation ADD CONSTRAINT r1plantcorrelation_plantstatus_id FOREIGN KEY (plantstatus_id) REFERENCES plantstatus(plantstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantlineage DROP CONSTRAINT r1plantlineage_childplantstatus_id;
ALTER TABLE ONLY plantlineage ADD CONSTRAINT r1plantlineage_childplantstatus_id FOREIGN KEY (childplantstatus_id) REFERENCES plantstatus(plantstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantname DROP CONSTRAINT r1plantname_reference_id;
ALTER TABLE ONLY plantname ADD CONSTRAINT r1plantname_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantstatus DROP CONSTRAINT r1plantstatus_plantconcept_id;
ALTER TABLE ONLY plantstatus ADD CONSTRAINT r1plantstatus_plantconcept_id FOREIGN KEY (plantconcept_id) REFERENCES plantconcept(plantconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantusage DROP CONSTRAINT r1plantusage_plantname_id;
ALTER TABLE ONLY plantusage ADD CONSTRAINT r1plantusage_plantname_id FOREIGN KEY (plantname_id) REFERENCES plantname(plantname_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plot DROP CONSTRAINT r1plot_reference_id;
ALTER TABLE ONLY plot ADD CONSTRAINT r1plot_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE projectcontributor DROP CONSTRAINT r1projectcontributor_project_id;
ALTER TABLE ONLY projectcontributor ADD CONSTRAINT r1projectcontributor_project_id FOREIGN KEY (project_id) REFERENCES project(project_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE reference DROP CONSTRAINT r1reference_referencejournal_id;
ALTER TABLE ONLY reference ADD CONSTRAINT r1reference_referencejournal_id FOREIGN KEY (referencejournal_id) REFERENCES referencejournal(referencejournal_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE referencealtident DROP CONSTRAINT r1referencealtident_reference_id;
ALTER TABLE ONLY referencealtident ADD CONSTRAINT r1referencealtident_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE referencecontributor DROP CONSTRAINT r1referencecontributor_reference_id;
ALTER TABLE ONLY referencecontributor ADD CONSTRAINT r1referencecontributor_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE referenceparty DROP CONSTRAINT r1referenceparty_currentparty_id;
ALTER TABLE ONLY referenceparty ADD CONSTRAINT r1referenceparty_currentparty_id FOREIGN KEY (currentparty_id) REFERENCES referenceparty(referenceparty_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE revision DROP CONSTRAINT r1revision_previousrevision_id;
ALTER TABLE ONLY revision ADD CONSTRAINT r1revision_previousrevision_id FOREIGN KEY (previousrevision_id) REFERENCES revision(revision_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE soilobs DROP CONSTRAINT r1soilobs_observation_id;
ALTER TABLE ONLY soilobs ADD CONSTRAINT r1soilobs_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE soiltaxon DROP CONSTRAINT r1soiltaxon_soilparent_id;
ALTER TABLE ONLY soiltaxon ADD CONSTRAINT r1soiltaxon_soilparent_id FOREIGN KEY (soilparent_id) REFERENCES soiltaxon(soiltaxon_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stemcount DROP CONSTRAINT r1stemcount_taxonimportance_id;
ALTER TABLE ONLY stemcount ADD CONSTRAINT r1stemcount_taxonimportance_id FOREIGN KEY (taxonimportance_id) REFERENCES taxonimportance(taxonimportance_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stemlocation DROP CONSTRAINT r1stemlocation_stemcount_id;
ALTER TABLE ONLY stemlocation ADD CONSTRAINT r1stemlocation_stemcount_id FOREIGN KEY (stemcount_id) REFERENCES stemcount(stemcount_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stratum DROP CONSTRAINT r1stratum_observation_id;
ALTER TABLE ONLY stratum ADD CONSTRAINT r1stratum_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stratummethod DROP CONSTRAINT r1stratummethod_reference_id;
ALTER TABLE ONLY stratummethod ADD CONSTRAINT r1stratummethod_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stratumtype DROP CONSTRAINT r1stratumtype_stratummethod_id;
ALTER TABLE ONLY stratumtype ADD CONSTRAINT r1stratumtype_stratummethod_id FOREIGN KEY (stratummethod_id) REFERENCES stratummethod(stratummethod_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxonalt DROP CONSTRAINT r1taxonalt_taxoninterpretation_id;
ALTER TABLE ONLY taxonalt ADD CONSTRAINT r1taxonalt_taxoninterpretation_id FOREIGN KEY (taxoninterpretation_id) REFERENCES taxoninterpretation(taxoninterpretation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxonimportance DROP CONSTRAINT r1taxonimportance_taxonobservation_id;
ALTER TABLE ONLY taxonimportance ADD CONSTRAINT r1taxonimportance_taxonobservation_id FOREIGN KEY (taxonobservation_id) REFERENCES taxonobservation(taxonobservation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r1taxoninterpretation_taxonobservation_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r1taxoninterpretation_taxonobservation_id FOREIGN KEY (taxonobservation_id) REFERENCES taxonobservation(taxonobservation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxonobservation DROP CONSTRAINT r1taxonobservation_observation_id;
ALTER TABLE ONLY taxonobservation ADD CONSTRAINT r1taxonobservation_observation_id FOREIGN KEY (observation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE telephone DROP CONSTRAINT r1telephone_party_id;
ALTER TABLE ONLY telephone ADD CONSTRAINT r1telephone_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE usercertification DROP CONSTRAINT r1usercertification_usr_id;
ALTER TABLE ONLY usercertification ADD CONSTRAINT r1usercertification_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userdataset DROP CONSTRAINT r1userdataset_usr_id;
ALTER TABLE ONLY userdataset ADD CONSTRAINT r1userdataset_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userdatasetitem DROP CONSTRAINT r1userdatasetitem_userdataset_id;
ALTER TABLE ONLY userdatasetitem ADD CONSTRAINT r1userdatasetitem_userdataset_id FOREIGN KEY (userdataset_id) REFERENCES userdataset(userdataset_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE usernotify DROP CONSTRAINT r1usernotify_usr_id;
ALTER TABLE ONLY usernotify ADD CONSTRAINT r1usernotify_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userpermission DROP CONSTRAINT r1userpermission_embargo_id;
ALTER TABLE ONLY userpermission ADD CONSTRAINT r1userpermission_embargo_id FOREIGN KEY (embargo_id) REFERENCES embargo(embargo_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userpreference DROP CONSTRAINT r1userpreference_usr_id;
ALTER TABLE ONLY userpreference ADD CONSTRAINT r1userpreference_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userquery DROP CONSTRAINT r1userquery_usr_id;
ALTER TABLE ONLY userquery ADD CONSTRAINT r1userquery_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userrecordowner DROP CONSTRAINT r1userrecordowner_usr_id;
ALTER TABLE ONLY userrecordowner ADD CONSTRAINT r1userrecordowner_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userregionalexp DROP CONSTRAINT r1userregionalexp_usercertification_id;
ALTER TABLE ONLY userregionalexp ADD CONSTRAINT r1userregionalexp_usercertification_id FOREIGN KEY (usercertification_id) REFERENCES usercertification(usercertification_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE usr DROP CONSTRAINT r1usr_party_id;
ALTER TABLE ONLY usr ADD CONSTRAINT r1usr_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE address DROP CONSTRAINT r2address_organization_id;
ALTER TABLE ONLY address ADD CONSTRAINT r2address_organization_id FOREIGN KEY (organization_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE classcontributor DROP CONSTRAINT r2classcontributor_party_id;
ALTER TABLE ONLY classcontributor ADD CONSTRAINT r2classcontributor_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commclass DROP CONSTRAINT r2commclass_classpublication_id;
ALTER TABLE ONLY commclass ADD CONSTRAINT r2commclass_classpublication_id FOREIGN KEY (classpublication_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commconcept DROP CONSTRAINT r2commconcept_reference_id;
ALTER TABLE ONLY commconcept ADD CONSTRAINT r2commconcept_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commcorrelation DROP CONSTRAINT r2commcorrelation_commconcept_id;
ALTER TABLE ONLY commcorrelation ADD CONSTRAINT r2commcorrelation_commconcept_id FOREIGN KEY (commconcept_id) REFERENCES commconcept(commconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE comminterpretation DROP CONSTRAINT r2comminterpretation_commconcept_id;
ALTER TABLE ONLY comminterpretation ADD CONSTRAINT r2comminterpretation_commconcept_id FOREIGN KEY (commconcept_id) REFERENCES commconcept(commconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commlineage DROP CONSTRAINT r2commlineage_childcommstatus_id;
ALTER TABLE ONLY commlineage ADD CONSTRAINT r2commlineage_childcommstatus_id FOREIGN KEY (childcommstatus_id) REFERENCES commstatus(commstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commstatus DROP CONSTRAINT r2commstatus_reference_id;
ALTER TABLE ONLY commstatus ADD CONSTRAINT r2commstatus_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commusage DROP CONSTRAINT r2commusage_commconcept_id;
ALTER TABLE ONLY commusage ADD CONSTRAINT r2commusage_commconcept_id FOREIGN KEY (commconcept_id) REFERENCES commconcept(commconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE namedplacecorrelation DROP CONSTRAINT r2namedplacecorrelation_childplace_id;
ALTER TABLE ONLY namedplacecorrelation ADD CONSTRAINT r2namedplacecorrelation_childplace_id FOREIGN KEY (childplace_id) REFERENCES namedplace(namedplace_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE note DROP CONSTRAINT r2note_party_id;
ALTER TABLE ONLY note ADD CONSTRAINT r2note_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observation DROP CONSTRAINT r2observation_plot_id;
ALTER TABLE ONLY observation ADD CONSTRAINT r2observation_plot_id FOREIGN KEY (plot_id) REFERENCES plot(plot_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationcontributor DROP CONSTRAINT r2observationcontributor_party_id;
ALTER TABLE ONLY observationcontributor ADD CONSTRAINT r2observationcontributor_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationsynonym DROP CONSTRAINT r2observationsynonym_primaryobservation_id;
ALTER TABLE ONLY observationsynonym ADD CONSTRAINT r2observationsynonym_primaryobservation_id FOREIGN KEY (primaryobservation_id) REFERENCES observation(observation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE partymember DROP CONSTRAINT r2partymember_childparty_id;
ALTER TABLE ONLY partymember ADD CONSTRAINT r2partymember_childparty_id FOREIGN KEY (childparty_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE place DROP CONSTRAINT r2place_namedplace_id;
ALTER TABLE ONLY place ADD CONSTRAINT r2place_namedplace_id FOREIGN KEY (namedplace_id) REFERENCES namedplace(namedplace_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantconcept DROP CONSTRAINT r2plantconcept_reference_id;
ALTER TABLE ONLY plantconcept ADD CONSTRAINT r2plantconcept_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantcorrelation DROP CONSTRAINT r2plantcorrelation_plantconcept_id;
ALTER TABLE ONLY plantcorrelation ADD CONSTRAINT r2plantcorrelation_plantconcept_id FOREIGN KEY (plantconcept_id) REFERENCES plantconcept(plantconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantlineage DROP CONSTRAINT r2plantlineage_parentplantstatus_id;
ALTER TABLE ONLY plantlineage ADD CONSTRAINT r2plantlineage_parentplantstatus_id FOREIGN KEY (parentplantstatus_id) REFERENCES plantstatus(plantstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantstatus DROP CONSTRAINT r2plantstatus_reference_id;
ALTER TABLE ONLY plantstatus ADD CONSTRAINT r2plantstatus_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantusage DROP CONSTRAINT r2plantusage_plantconcept_id;
ALTER TABLE ONLY plantusage ADD CONSTRAINT r2plantusage_plantconcept_id FOREIGN KEY (plantconcept_id) REFERENCES plantconcept(plantconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plot DROP CONSTRAINT r2plot_parent_id;
ALTER TABLE ONLY plot ADD CONSTRAINT r2plot_parent_id FOREIGN KEY (parent_id) REFERENCES plot(plot_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE projectcontributor DROP CONSTRAINT r2projectcontributor_party_id;
ALTER TABLE ONLY projectcontributor ADD CONSTRAINT r2projectcontributor_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE referencecontributor DROP CONSTRAINT r2referencecontributor_referenceparty_id;
ALTER TABLE ONLY referencecontributor ADD CONSTRAINT r2referencecontributor_referenceparty_id FOREIGN KEY (referenceparty_id) REFERENCES referenceparty(referenceparty_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stratum DROP CONSTRAINT r2stratum_stratumtype_id;
ALTER TABLE ONLY stratum ADD CONSTRAINT r2stratum_stratumtype_id FOREIGN KEY (stratumtype_id) REFERENCES stratumtype(stratumtype_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxonalt DROP CONSTRAINT r2taxonalt_plantconcept_id;
ALTER TABLE ONLY taxonalt ADD CONSTRAINT r2taxonalt_plantconcept_id FOREIGN KEY (plantconcept_id) REFERENCES plantconcept(plantconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxonimportance DROP CONSTRAINT r2taxonimportance_stratum_id;
ALTER TABLE ONLY taxonimportance ADD CONSTRAINT r2taxonimportance_stratum_id FOREIGN KEY (stratum_id) REFERENCES stratum(stratum_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r2taxoninterpretation_stemlocation_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r2taxoninterpretation_stemlocation_id FOREIGN KEY (stemlocation_id) REFERENCES stemlocation(stemlocation_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxonobservation DROP CONSTRAINT r2taxonobservation_reference_id;
ALTER TABLE ONLY taxonobservation ADD CONSTRAINT r2taxonobservation_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE userpermission DROP CONSTRAINT r2userpermission_usr_id;
ALTER TABLE ONLY userpermission ADD CONSTRAINT r2userpermission_usr_id FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE classcontributor DROP CONSTRAINT r3classcontributor_role_id;
ALTER TABLE ONLY classcontributor ADD CONSTRAINT r3classcontributor_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE comminterpretation DROP CONSTRAINT r3comminterpretation_commauthority_id;
ALTER TABLE ONLY comminterpretation ADD CONSTRAINT r3comminterpretation_commauthority_id FOREIGN KEY (commauthority_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commstatus DROP CONSTRAINT r3commstatus_commparent_id;
ALTER TABLE ONLY commstatus ADD CONSTRAINT r3commstatus_commparent_id FOREIGN KEY (commparent_id) REFERENCES commconcept(commconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commusage DROP CONSTRAINT r3commusage_party_id;
ALTER TABLE ONLY commusage ADD CONSTRAINT r3commusage_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE note DROP CONSTRAINT r3note_role_id;
ALTER TABLE ONLY note ADD CONSTRAINT r3note_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observation DROP CONSTRAINT r3observation_project_id;
ALTER TABLE ONLY observation ADD CONSTRAINT r3observation_project_id FOREIGN KEY (project_id) REFERENCES project(project_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationcontributor DROP CONSTRAINT r3observationcontributor_role_id;
ALTER TABLE ONLY observationcontributor ADD CONSTRAINT r3observationcontributor_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationsynonym DROP CONSTRAINT r3observationsynonym_party_id;
ALTER TABLE ONLY observationsynonym ADD CONSTRAINT r3observationsynonym_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE partymember DROP CONSTRAINT r3partymember_role_id;
ALTER TABLE ONLY partymember ADD CONSTRAINT r3partymember_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantstatus DROP CONSTRAINT r3plantstatus_plantparent_id;
ALTER TABLE ONLY plantstatus ADD CONSTRAINT r3plantstatus_plantparent_id FOREIGN KEY (plantparent_id) REFERENCES plantconcept(plantconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantusage DROP CONSTRAINT r3plantusage_party_id;
ALTER TABLE ONLY plantusage ADD CONSTRAINT r3plantusage_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE projectcontributor DROP CONSTRAINT r3projectcontributor_role_id;
ALTER TABLE ONLY projectcontributor ADD CONSTRAINT r3projectcontributor_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE stratum DROP CONSTRAINT r3stratum_stratummethod_id;
ALTER TABLE ONLY stratum ADD CONSTRAINT r3stratum_stratummethod_id FOREIGN KEY (stratummethod_id) REFERENCES stratummethod(stratummethod_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r3taxoninterpretation_plantconcept_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r3taxoninterpretation_plantconcept_id FOREIGN KEY (plantconcept_id) REFERENCES plantconcept(plantconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commstatus DROP CONSTRAINT r4commstatus_party_id;
ALTER TABLE ONLY commstatus ADD CONSTRAINT r4commstatus_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE commusage DROP CONSTRAINT r4commusage_commstatus_id;
ALTER TABLE ONLY commusage ADD CONSTRAINT r4commusage_commstatus_id FOREIGN KEY (commstatus_id) REFERENCES commstatus(commstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observation DROP CONSTRAINT r4observation_covermethod_id;
ALTER TABLE ONLY observation ADD CONSTRAINT r4observation_covermethod_id FOREIGN KEY (covermethod_id) REFERENCES covermethod(covermethod_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observationsynonym DROP CONSTRAINT r4observationsynonym_role_id;
ALTER TABLE ONLY observationsynonym ADD CONSTRAINT r4observationsynonym_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantstatus DROP CONSTRAINT r4plantstatus_party_id;
ALTER TABLE ONLY plantstatus ADD CONSTRAINT r4plantstatus_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE plantusage DROP CONSTRAINT r4plantusage_plantstatus_id;
ALTER TABLE ONLY plantusage ADD CONSTRAINT r4plantusage_plantstatus_id FOREIGN KEY (plantstatus_id) REFERENCES plantstatus(plantstatus_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r4taxoninterpretation_plantname_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r4taxoninterpretation_plantname_id FOREIGN KEY (plantname_id) REFERENCES plantname(plantname_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observation DROP CONSTRAINT r5observation_stratummethod_id;
ALTER TABLE ONLY observation ADD CONSTRAINT r5observation_stratummethod_id FOREIGN KEY (stratummethod_id) REFERENCES stratummethod(stratummethod_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r5taxoninterpretation_party_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r5taxoninterpretation_party_id FOREIGN KEY (party_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE observation DROP CONSTRAINT r6observation_soiltaxon_id;
ALTER TABLE ONLY observation ADD CONSTRAINT r6observation_soiltaxon_id FOREIGN KEY (soiltaxon_id) REFERENCES soiltaxon(soiltaxon_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r6taxoninterpretation_role_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r6taxoninterpretation_role_id FOREIGN KEY (role_id) REFERENCES aux_role(role_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r7taxoninterpretation_reference_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r7taxoninterpretation_reference_id FOREIGN KEY (reference_id) REFERENCES reference(reference_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r8taxoninterpretation_collector_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r8taxoninterpretation_collector_id FOREIGN KEY (collector_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE taxoninterpretation DROP CONSTRAINT r9taxoninterpretation_museum_id;
ALTER TABLE ONLY taxoninterpretation ADD CONSTRAINT r9taxoninterpretation_museum_id FOREIGN KEY (museum_id) REFERENCES party(party_id) ON UPDATE CASCADE ON DELETE CASCADE;

-- Remove private data that should not be publicly visible
ALTER TABLE plot DROP COLUMN reallatitude;
ALTER TABLE plot DROP COLUMN reallongitude;

DELETE FROM plot WHERE plot_id IN (
    SELECT plot_id FROM embargo WHERE embargostop > now()
);
-- Do not delete confidentialitystatus >= 4 whose embargos have already expired
-- This corresponds to location/public/full embargos (http://vegbank.org/vegbank/views/dba_tabledescription_detail.jsp?view=detail&wparam=plot&entity=dba_tabledescription&where=where_tablename#confidentialityStatus)

-- Prevent "column name specified more than once" errors when tables are joined
ALTER TABLE taxonobservation RENAME reference_id  TO taxonobservation_reference_id;
ALTER TABLE taxonobservation RENAME accessioncode  TO taxonobservation_accessioncode;
ALTER TABLE taxoninterpretation RENAME party_id  TO taxoninterpretation_party_id;
ALTER TABLE taxoninterpretation RENAME plantname_id  TO taxoninterpretation_plantname_id;
ALTER TABLE taxoninterpretation RENAME accessioncode  TO taxoninterpretation_accessioncode;
ALTER TABLE party RENAME d_obscount  TO party_d_obscount;
ALTER TABLE plantconcept RENAME reference_id  TO plantconcept_reference_id;
ALTER TABLE plantconcept RENAME accessioncode  TO plantconcept_accessioncode;
