ALTER TABLE "contract_type_person"
    ADD FOREIGN KEY ("person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

UPDATE
    hw
SET
    parent = NULL
WHERE
    parent = 0;

ALTER TABLE "hw"
    ADD FOREIGN KEY ("parent") REFERENCES "hw" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "hw"
    ADD FOREIGN KEY ("brand") REFERENCES "hw_brand" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

ALTER TABLE "hw_barcode"
    ADD FOREIGN KEY ("hw") REFERENCES "hw" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "hw_log"
    ADD FOREIGN KEY ("status") REFERENCES "hw_status" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "hw_log"
    ADD FOREIGN KEY ("owner") REFERENCES "person" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

ALTER TABLE "hw_log"
    ADD FOREIGN KEY ("author") REFERENCES "person" ("id") ON DELETE SET NULL ON
    UPDATE
        CASCADE;

ALTER TABLE "hw_log"
    ADD FOREIGN KEY ("location_hw") REFERENCES "hw" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

ALTER TABLE "hw_log"
    ADD FOREIGN KEY ("location_person") REFERENCES "person" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

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

ALTER TABLE "hw_log"
    ADD FOREIGN KEY ("location_stock") REFERENCES "hw_stock" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

DELETE FROM "photo_relation"
WHERE "photo" = '122';

DELETE FROM "photo_relation"
WHERE "photo" = '123';

-- neexistuje záznam s photo.id = 122
ALTER TABLE "photo_relation"
    ADD FOREIGN KEY ("photo") REFERENCES "photo" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "photo_relation"
    ADD FOREIGN KEY ("creator") REFERENCES "person" ("id") ON DELETE RESTRICT ON
    UPDATE
        CASCADE;

ALTER TABLE "property"
    ADD FOREIGN KEY ("sk_obec") REFERENCES "sk_obec" ("kod") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "property"
    ADD FOREIGN KEY ("sk_ulica") REFERENCES "sk_ulica" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

ALTER TABLE "property_part"
    ADD FOREIGN KEY ("property") REFERENCES "property" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

ALTER TABLE "property_person"
    ADD FOREIGN KEY ("property") REFERENCES "property" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "property_person"
    ADD FOREIGN KEY ("property_part") REFERENCES "property_part" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

ALTER TABLE "property_person"
    ADD FOREIGN KEY ("person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifaccess"
    ADD FOREIGN KEY ("tarif") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifaccess"
    ADD FOREIGN KEY ("pid") REFERENCES "person" ("pid") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

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

ALTER TABLE "tarifitem"
    ADD FOREIGN KEY ("hw_brand") REFERENCES "hw_brand" ("id") ON
    UPDATE
        CASCADE;

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

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

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

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

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

ALTER TABLE "tarifitem_task_type"
    ADD FOREIGN KEY ("task_type") REFERENCES "task_type" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

ALTER TABLE "tarifitemacct"
    ADD FOREIGN KEY ("person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

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

ALTER TABLE "tarifitemrate"
    ADD FOREIGN KEY ("supplier") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifitemrate"
    ADD FOREIGN KEY ("tarifitemgroup") REFERENCES "tarifitemgroup" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifpartner"
    ADD FOREIGN KEY ("tarif_customer") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifpartner"
    ADD FOREIGN KEY ("tarif_partner") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifpartner"
    ADD FOREIGN KEY ("mediator") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "tarifpartner"
    ADD FOREIGN KEY ("referrer") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("status") REFERENCES "task_status" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("author") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("type") REFERENCES "task_type" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("tarif_supplier") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("tarif_mediator") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("tarif_customer") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("parent") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("priority") REFERENCES "task_priority" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("final_calc") REFERENCES "task_calc" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("customer") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("manager1") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("manager2") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("location_hw") REFERENCES "hw" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("location_person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task"
    ADD FOREIGN KEY ("location_stock") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc"
    ADD FOREIGN KEY ("task") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc"
    ADD FOREIGN KEY ("author") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc"
    ADD FOREIGN KEY ("calc_type") REFERENCES "task_calc_type" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc"
    ADD FOREIGN KEY ("status") REFERENCES "task_calc_status" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc"
    ADD FOREIGN KEY ("approvator_person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc_item"
    ADD FOREIGN KEY ("calc") REFERENCES "task_calc_item" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc_item"
    ADD FOREIGN KEY ("hw") REFERENCES "hw" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_calc_item"
    ADD FOREIGN KEY ("supplier") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_event"
    ADD FOREIGN KEY ("task") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_event"
    ADD FOREIGN KEY ("person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_event"
    ADD FOREIGN KEY ("event_type") REFERENCES "task_event_type" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_event"
    ADD FOREIGN KEY ("accept_person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_location"
    ADD FOREIGN KEY ("task") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_location"
    ADD FOREIGN KEY ("location_person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_person"
    ADD FOREIGN KEY ("task") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_person"
    ADD FOREIGN KEY ("person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_person"
    ADD FOREIGN KEY ("cancel_reason") REFERENCES "task_person_cancel_reason" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_shift"
    ADD FOREIGN KEY ("type") REFERENCES "task_shift_type" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_shift"
    ADD FOREIGN KEY ("person") REFERENCES "person" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_tag"
    ADD FOREIGN KEY ("parent") REFERENCES "task_tag" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_tag_relation"
    ADD FOREIGN KEY ("task") REFERENCES "task" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_tag_relation"
    ADD FOREIGN KEY ("tag") REFERENCES "task_tag_relation" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

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

ALTER TABLE "task_tarifitem_role"
    ADD FOREIGN KEY ("acl_role") REFERENCES "acl_role" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_type"
    ADD FOREIGN KEY ("tarif_supplier") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_type"
    ADD FOREIGN KEY ("tarif_mediator") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_type"
    ADD FOREIGN KEY ("tarif_customer") REFERENCES "tarif" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

ALTER TABLE "task_type"
    ADD FOREIGN KEY ("priority") REFERENCES "task_priority" ("id") ON DELETE CASCADE ON
    UPDATE
        CASCADE;

