ALTER TABLE "contract_item"
    ADD FOREIGN KEY ("tarifitem") REFERENCES "tarifitem" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

CREATE INDEX "contract_item_evid" ON "contract_item" ("evid");

CREATE INDEX "contract_item_tarifitem" ON "contract_item" ("tarifitem");

ALTER TABLE "contract_item"
    ADD FOREIGN KEY ("billinterval") REFERENCES "billinterval" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

CREATE INDEX "contract_item_billinterval" ON "contract_item" ("billinterval");

ALTER TABLE "contract_item"
    ADD FOREIGN KEY ("unit") REFERENCES "tarifitemunit" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifitem"
    ADD FOREIGN KEY ("acl_role_customer") REFERENCES "acl_role" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

ALTER TABLE "cpe"
    ADD FOREIGN KEY ("provsrv") REFERENCES "cpe_provsrv" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE
            ALTER TABLE "ipfon_phonebook_user"
            ADD CONSTRAINT "ipfon_phonebook_user_username" UNIQUE ("username");

ALTER TABLE "ipfon_phonebook_user"
    ADD FOREIGN KEY ("include_pbx") REFERENCES "pbx" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

ALTER TABLE "cpe"
    ADD FOREIGN KEY ("type") REFERENCES "cpe_type" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

ALTER TABLE "cpe"
    ADD FOREIGN KEY ("phonebook_user") REFERENCES "ipfon_phonebook_user" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_acl"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_acl_rule"
    ADD FOREIGN KEY ("acl") REFERENCES "pbx_acl" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_badge"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

CREATE INDEX "pbx_badge_phonebook_group_id" ON "pbx_badge_phonebook_group" ("id");

ALTER TABLE "pbx_badge_phonebook_group"
    ADD CONSTRAINT "pbx_badge_phonebook_group_pbx_badge_phonebook_group" UNIQUE ("pbx", "badge", "phonebook_group");

ALTER TABLE "pbx_badge_phonebook_group"
    ADD FOREIGN KEY ("badge") REFERENCES "pbx_badge" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_callgroup"
    ADD CONSTRAINT "pbx_callgroup_sysname" UNIQUE ("sysname");

ALTER TABLE "pbx_callscript"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE CREATE INDEX "pbx_exten_id" ON "pbx_exten" ("id");

ALTER TABLE "pbx_exten"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_exten"
    ADD FOREIGN KEY ("cpe") REFERENCES "cpe" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_exten"
    ADD FOREIGN KEY ("callscript") REFERENCES "pbx_callscript" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_exten_callgroup"
    ADD FOREIGN KEY ("exten") REFERENCES "pbx_exten" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_host"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_instance"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_instance"
    ADD FOREIGN KEY ("softswitch") REFERENCES "ipfon_softswitch" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_line"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_missedcall"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_perm"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_perm_include"
    ADD FOREIGN KEY ("perm") REFERENCES "pbx_perm" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_perm_rule"
    ADD FOREIGN KEY ("perm") REFERENCES "pbx_perm" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_queue"
    ADD CONSTRAINT "pbx_queue_sysname" UNIQUE ("sysname");

ALTER TABLE "pbx_queue"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_routing"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_sound"
    ADD FOREIGN KEY ("pbx") REFERENCES "pbx" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_sound_lang"
    ADD FOREIGN KEY ("sound") REFERENCES "pbx_sound" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "pbx_sound"
    ADD CONSTRAINT "pbx_sound_pbx_sysname" UNIQUE ("pbx", "sysname");

ALTER TABLE "pbx_perm_include"
    ADD FOREIGN KEY ("member_perm") REFERENCES "pbx_perm" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

