BEGIN;
CREATE TABLE temp AS SELECT * FROM _product_measure_data;
DROP TABLE _product_measure_data;
CREATE TABLE _product_measure_data (
    product int,
    product_measure_type int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    measure numeric(12,4),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_measure_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX product_measure_product_measure_type_idx ON _product_measure_data(product_measure_type);
CREATE INDEX product_measure_product_idx ON _product_measure_data(product);
INSERT INTO _product_measure_data (product, product_measure_type, _start_t, _end_t, measure, _table, id) SELECT product, product_measure_type, _start_t, _end_t, measure, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_file_data;
DROP TABLE _product_file_data;
CREATE TABLE _product_file_data (
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    file int,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_file_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX product_file_product_idx ON _product_file_data(product);
CREATE INDEX product_file_file_idx ON _product_file_data(file);
INSERT INTO _product_file_data (product, _start_t, _end_t, file, _table, id) SELECT product, _start_t, _end_t, file, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _alien_entity_data;
DROP TABLE _alien_entity_data;
CREATE TABLE _alien_entity_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_alien_entity_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX alien_entity_entity_idx ON _alien_entity_data(entity);
INSERT INTO _alien_entity_data (_start_t, _end_t, _table, id, entity) SELECT _start_t, _end_t, _table, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM barter;
DROP TABLE barter;
CREATE TABLE barter (
    id int NOT NULL DEFAULT nextval('barter_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO barter (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_measure_type_data;
DROP TABLE _product_measure_type_data;
CREATE TABLE _product_measure_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_measure_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _product_measure_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM movement_type;
DROP TABLE movement_type;
CREATE TABLE movement_type (
    id int NOT NULL DEFAULT nextval('movement_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO movement_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_pos;
DROP TABLE own_pos;
CREATE TABLE own_pos (
    id int NOT NULL DEFAULT nextval('own_pos_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO own_pos (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM erm_task;
DROP TABLE erm_task;
CREATE TABLE erm_task (
    erm int,
    task int,
    id int NOT NULL DEFAULT nextval('erm_task_id_seq'),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    PRIMARY KEY (id)
);
CREATE INDEX erm_task_erm_idx ON erm_task(erm);
CREATE INDEX erm_task_task_idx ON erm_task(task);
INSERT INTO erm_task (erm, task, id) SELECT erm, task, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity;
DROP TABLE entity;
CREATE TABLE entity (
    id int NOT NULL DEFAULT nextval('entity_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM persona_tax_profile;
DROP TABLE persona_tax_profile;
CREATE TABLE persona_tax_profile (
    id int NOT NULL DEFAULT nextval('persona_tax_profile_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO persona_tax_profile (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM task_type;
DROP TABLE task_type;
CREATE TABLE task_type (
    id int NOT NULL DEFAULT nextval('task_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO task_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM credit_card_movement;
DROP TABLE credit_card_movement;
CREATE TABLE credit_card_movement (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    movement bigint,
    id bigint NOT NULL DEFAULT nextval('credit_card_movement_id_seq'),
    entry bigint,
    plan bigint,
    PRIMARY KEY (id)
);
CREATE INDEX credit_card_movement_plan_idx ON credit_card_movement(plan);
CREATE INDEX credit_card_movement_movement_idx ON credit_card_movement(movement);
INSERT INTO credit_card_movement (movement, id, entry, plan) SELECT movement, id, entry, plan FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _category_entity_type_data;
DROP TABLE _category_entity_type_data;
CREATE TABLE _category_entity_type_data (
    category_type int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    entity_type int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_category_entity_type_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX category_entity_type_entity_type_idx ON _category_entity_type_data(entity_type);
CREATE INDEX category_entity_type_category_type_idx ON _category_entity_type_data(category_type);
INSERT INTO _category_entity_type_data (category_type, _start_t, _end_t, entity_type, _table, id) SELECT category_type, _start_t, _end_t, entity_type, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_document_proceeding;
DROP TABLE alien_document_proceeding;
CREATE TABLE alien_document_proceeding (
    amount numeric(12,4),
    proceeding bigint,
    entry bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id bigint NOT NULL DEFAULT nextval('alien_document_proceeding_id_seq'),
    alien_document bigint,
    PRIMARY KEY (id)
);
CREATE INDEX alien_document_proceeding_proceeding_idx ON alien_document_proceeding(proceeding);
CREATE INDEX alien_document_proceeding_alien_document_idx ON alien_document_proceeding(alien_document);
INSERT INTO alien_document_proceeding (amount, proceeding, entry, id, alien_document) SELECT amount, proceeding, entry, id, alien_document FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _bank_account_data;
DROP TABLE _bank_account_data;
CREATE TABLE _bank_account_data (
    credit numeric(12,4),
    credit_timestamp timestamp,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    overdraft numeric(12,4),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    bank_branch int,
    id bigint NOT NULL DEFAULT nextval('_bank_account_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX bank_account_bank_branch_idx ON _bank_account_data(bank_branch);
INSERT INTO _bank_account_data (credit, credit_timestamp, _start_t, _end_t, overdraft, _table, bank_branch, id) SELECT credit, credit_timestamp, _start_t, _end_t, overdraft, _table, bank_branch, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _account_plan_data;
DROP TABLE _account_plan_data;
CREATE TABLE _account_plan_data (
    balance numeric(12,4),
    code varchar(40) NOT NULL,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(40) NOT NULL,
    parent int,
    _state int DEFAULT -1 NOT NULL,
    chargeable boolean,
    path varchar(255),
    _table int NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_account_plan_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX account_plan_parent_idx ON _account_plan_data(parent);
INSERT INTO _account_plan_data (balance, code, _start_t, name, parent, chargeable, path, _table, id, _end_t) SELECT balance, code, _start_t, name, parent, chargeable, path, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _employee_comm_client_data;
DROP TABLE _employee_comm_client_data;
CREATE TABLE _employee_comm_client_data (
    commission int,
    client int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    employee int,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_employee_comm_client_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX employee_comm_client_client_idx ON _employee_comm_client_data(client);
CREATE INDEX employee_comm_client_commission_idx ON _employee_comm_client_data(commission);
CREATE INDEX employee_comm_client_employee_idx ON _employee_comm_client_data(employee);
INSERT INTO _employee_comm_client_data (commission, client, _start_t, _end_t, employee, _table, id) SELECT commission, client, _start_t, _end_t, employee, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_classification;
DROP TABLE product_classification;
CREATE TABLE product_classification (
    id int NOT NULL DEFAULT nextval('product_classification_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_classification (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM bank;
DROP TABLE bank;
CREATE TABLE bank (
    id int NOT NULL DEFAULT nextval('bank_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO bank (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_data;
DROP TABLE _product_data;
CREATE TABLE _product_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    commission int,
    country int,
    salable boolean,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    product_classification int,
    is_perishable boolean,
    canonical_name varchar(80),
    location int,
    _table int NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX product_country_idx ON _product_data(country);
CREATE INDEX product_product_classification_idx ON _product_data(product_classification);
CREATE INDEX product_commission_idx ON _product_data(commission);
CREATE INDEX product_location_idx ON _product_data(location);
INSERT INTO _product_data (_start_t, _end_t, commission, country, salable, account_plan, product_classification, is_perishable, canonical_name, location, _table, id) SELECT _start_t, _end_t, commission, country, salable, account_plan, product_classification, is_perishable, canonical_name, location, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM document4document;
DROP TABLE document4document;
CREATE TABLE document4document (
    own_document bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('document4document_id_seq'),
    alien_document bigint,
    PRIMARY KEY (id)
);
CREATE INDEX document4document_alien_document_idx ON document4document(alien_document);
CREATE INDEX document4document_own_document_idx ON document4document(own_document);
INSERT INTO document4document (own_document, entry, id, alien_document) SELECT own_document, entry, id, alien_document FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM employee;
DROP TABLE employee;
CREATE TABLE employee (
    id int NOT NULL DEFAULT nextval('employee_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO employee (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM logical_structure;
DROP TABLE logical_structure;
CREATE TABLE logical_structure (
    id int NOT NULL DEFAULT nextval('logical_structure_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO logical_structure (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM address_type;
DROP TABLE address_type;
CREATE TABLE address_type (
    id int NOT NULL DEFAULT nextval('address_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO address_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _employee_comm_prod_data;
DROP TABLE _employee_comm_prod_data;
CREATE TABLE _employee_comm_prod_data (
    commission int,
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    employee int,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_employee_comm_prod_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX employee_comm_prod_product_idx ON _employee_comm_prod_data(product);
CREATE INDEX employee_comm_prod_commission_idx ON _employee_comm_prod_data(commission);
CREATE INDEX employee_comm_prod_employee_idx ON _employee_comm_prod_data(employee);
INSERT INTO _employee_comm_prod_data (commission, product, _start_t, _end_t, employee, _table, id) SELECT commission, product, _start_t, _end_t, employee, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_relation_data;
DROP TABLE _entity_relation_data;
CREATE TABLE _entity_relation_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    entity_from int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_relation_data_id_seq'),
    entity_to int,
    PRIMARY KEY (id)
);
CREATE INDEX entity_relation_entity_from_idx ON _entity_relation_data(entity_from);
CREATE INDEX entity_relation_entity_to_idx ON _entity_relation_data(entity_to);
INSERT INTO _entity_relation_data (_start_t, _end_t, entity_from, _table, id, entity_to) SELECT _start_t, _end_t, entity_from, _table, id, entity_to FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_store;
DROP TABLE product_store;
CREATE TABLE product_store (
    id bigint NOT NULL DEFAULT nextval('product_store_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_store (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_item_tax;
DROP TABLE own_item_tax;
CREATE TABLE own_item_tax (
    amount numeric(12,4),
    entry bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    tax bigint,
    own_item bigint,
    id bigint NOT NULL DEFAULT nextval('own_item_tax_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX own_item_tax_own_item_idx ON own_item_tax(own_item);
CREATE INDEX own_item_tax_tax_idx ON own_item_tax(tax);
INSERT INTO own_item_tax (amount, entry, tax, own_item, id) SELECT amount, entry, tax, own_item, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM cheque_movement;
DROP TABLE cheque_movement;
CREATE TABLE cheque_movement (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    movement bigint,
    id bigint NOT NULL DEFAULT nextval('cheque_movement_id_seq'),
    entry bigint,
    cheque bigint,
    PRIMARY KEY (id)
);
CREATE INDEX cheque_movement_cheque_idx ON cheque_movement(cheque);
CREATE INDEX cheque_movement_movement_idx ON cheque_movement(movement);
INSERT INTO cheque_movement (movement, id, entry, cheque) SELECT movement, id, entry, cheque FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _document_type_data;
DROP TABLE _document_type_data;
CREATE TABLE _document_type_data (
    code char(4),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    description text,
    name varchar(80),
    id bigint NOT NULL DEFAULT nextval('_document_type_data_id_seq'),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    sign boolean DEFAULT NULL,
    PRIMARY KEY (id)
);
INSERT INTO _document_type_data (code, _start_t, description, name, id, _table, account_plan, _end_t, sign) SELECT code, _start_t, description, name, id, _table, account_plan, _end_t, sign FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_name_data;
DROP TABLE _product_name_data;
CREATE TABLE _product_name_data (
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    product_name_type int,
    maximum_length int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_name_data_id_seq'),
    _end_t timestamp,
    name varchar(80),
    PRIMARY KEY (id)
);
CREATE INDEX product_name_product_idx ON _product_name_data(product);
CREATE INDEX product_name_product_name_type_idx ON _product_name_data(product_name_type);
INSERT INTO _product_name_data (product, _start_t, product_name_type, maximum_length, _table, id, _end_t, name) SELECT product, _start_t, product_name_type, maximum_length, _table, id, _end_t, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _tax_formula_data;
DROP TABLE _tax_formula_data;
CREATE TABLE _tax_formula_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    graft text,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_tax_formula_data_id_seq'),
    from_profile int,
    to_profile int,
    PRIMARY KEY (id)
);
CREATE INDEX tax_formula_to_profile_idx ON _tax_formula_data(to_profile);
CREATE INDEX tax_formula_from_profile_idx ON _tax_formula_data(from_profile);
INSERT INTO _tax_formula_data (_start_t, _end_t, graft, _table, id, from_profile, to_profile) SELECT _start_t, _end_t, graft, _table, id, from_profile, to_profile FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_type_uid_conf;
DROP TABLE entity_type_uid_conf;
CREATE TABLE entity_type_uid_conf (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id int NOT NULL DEFAULT nextval('entity_type_uid_conf_id_seq'),
    entity_uid_type bigint,
    entity_type bigint,
    PRIMARY KEY (id)
);
CREATE INDEX entity_type_uid_conf_entity_type_idx ON entity_type_uid_conf(entity_type);
CREATE INDEX entity_type_uid_conf_entity_uid_type_idx ON entity_type_uid_conf(entity_uid_type);
INSERT INTO entity_type_uid_conf (id, entity_uid_type, entity_type) SELECT id, entity_uid_type, entity_type FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_product_item;
DROP TABLE alien_product_item;
CREATE TABLE alien_product_item (
    product bigint,
    account_plan bigint,
    entry bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    qty numeric(12,4),
    id bigint NOT NULL DEFAULT nextval('alien_product_item_id_seq'),
    unit_price numeric(12,4),
    alien_item bigint,
    PRIMARY KEY (id)
);
CREATE INDEX alien_product_item_product_idx ON alien_product_item(product);
CREATE INDEX alien_product_item_alien_item_idx ON alien_product_item(alien_item);
INSERT INTO alien_product_item (product, account_plan, entry, qty, id, unit_price, alien_item) SELECT product, account_plan, entry, qty, id, unit_price, alien_item FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM category_entity_type;
DROP TABLE category_entity_type;
CREATE TABLE category_entity_type (
    id int NOT NULL DEFAULT nextval('category_entity_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO category_entity_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM ticket;
DROP TABLE ticket;
CREATE TABLE ticket (
    id int NOT NULL DEFAULT nextval('ticket_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO ticket (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM document_type;
DROP TABLE document_type;
CREATE TABLE document_type (
    id int NOT NULL DEFAULT nextval('document_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO document_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _tax_data;
DROP TABLE _tax_data;
CREATE TABLE _tax_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    id bigint NOT NULL DEFAULT nextval('_tax_data_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO _tax_data (_start_t, name, _table, account_plan, _end_t, id) SELECT _start_t, name, _table, account_plan, _end_t, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _credit_card_data;
DROP TABLE _credit_card_data;
CREATE TABLE _credit_card_data (
    _table int NOT NULL,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    accreditation_account int,
    ownid int,
    accreditation_date timestamp,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    id bigint NOT NULL DEFAULT nextval('_credit_card_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX credit_card_accreditation_account_idx ON _credit_card_data(accreditation_account);
INSERT INTO _credit_card_data (_table, _start_t, name, accreditation_account, ownid, accreditation_date, account_plan, _end_t, id) SELECT _table, _start_t, name, accreditation_account, ownid, accreditation_date, account_plan, _end_t, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM bank_account;
DROP TABLE bank_account;
CREATE TABLE bank_account (
    id int NOT NULL DEFAULT nextval('bank_account_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO bank_account (id) SELECT id FROM temp;
DROP TABLE temp;
-- CREATE TABLE temp AS SELECT * FROM task_recipient;
DROP TABLE task_recipient;
CREATE TABLE task_recipient (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    task int,
    id int NOT NULL DEFAULT nextval('task_recipient_id_seq'),
    to_make_to bigint,
    PRIMARY KEY (id)
);
CREATE INDEX task_recipient_to_make_to_idx ON task_recipient(to_make_to);
CREATE INDEX task_recipient_task_idx ON task_recipient(task);
-- INSERT INTO task_recipient (task, id, to_make_to) SELECT task, id, to_make_to FROM temp;
-- DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_code_type;
DROP TABLE product_code_type;
CREATE TABLE product_code_type (
    id int NOT NULL DEFAULT nextval('product_code_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_code_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_attribute_data;
DROP TABLE _entity_attribute_data;
CREATE TABLE _entity_attribute_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    category_attribute int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_attribute_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX entity_attribute_entity_idx ON _entity_attribute_data(entity);
CREATE INDEX entity_attribute_category_attribute_idx ON _entity_attribute_data(category_attribute);
INSERT INTO _entity_attribute_data (_start_t, _end_t, category_attribute, _table, id, entity) SELECT _start_t, _end_t, category_attribute, _table, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_descriptive_item;
DROP TABLE alien_descriptive_item;
CREATE TABLE alien_descriptive_item (
    amount numeric(12,4),
    description text,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('alien_descriptive_item_id_seq'),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    account_plan bigint,
    alien_item bigint,
    PRIMARY KEY (id)
);
CREATE INDEX alien_descriptive_item_alien_item_idx ON alien_descriptive_item(alien_item);
INSERT INTO alien_descriptive_item (amount, description, entry, id, account_plan, alien_item) SELECT amount, description, entry, id, account_plan, alien_item FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM third_party_cheque_tax;
DROP TABLE third_party_cheque_tax;
CREATE TABLE third_party_cheque_tax (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    third_party_cheque bigint,
    tax bigint,
    id bigint NOT NULL DEFAULT nextval('third_party_cheque_tax_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX third_party_cheque_tax_third_party_cheque_idx ON third_party_cheque_tax(third_party_cheque);
CREATE INDEX third_party_cheque_tax_tax_idx ON third_party_cheque_tax(tax);
INSERT INTO third_party_cheque_tax (third_party_cheque, tax, id) SELECT third_party_cheque, tax, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM tax_formula;
DROP TABLE tax_formula;
CREATE TABLE tax_formula (
    id int NOT NULL DEFAULT nextval('tax_formula_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO tax_formula (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM relation_types;
DROP TABLE relation_types;
CREATE TABLE relation_types (
    id int NOT NULL DEFAULT nextval('relation_types_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO relation_types (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_doc4own_doc;
DROP TABLE own_doc4own_doc;
CREATE TABLE own_doc4own_doc (
    document_to bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id int NOT NULL DEFAULT nextval('own_doc4own_doc_id_seq'),
    relation_type int,
    document_from bigint,
    PRIMARY KEY (id)
);
CREATE INDEX own_doc4own_doc_relation_type_idx ON own_doc4own_doc(relation_type);
CREATE INDEX own_doc4own_doc_document_from_idx ON own_doc4own_doc(document_from);
CREATE INDEX own_doc4own_doc_document_to_idx ON own_doc4own_doc(document_to);
INSERT INTO own_doc4own_doc (document_to, id, relation_type, document_from) SELECT document_to, id, relation_type, document_from FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_type_data;
DROP TABLE _entity_type_data;
CREATE TABLE _entity_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    parent int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_type_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX entity_type_parent_idx ON _entity_type_data(parent);
INSERT INTO _entity_type_data (_start_t, name, parent, _table, id, _end_t) SELECT _start_t, name, parent, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_document_tax;
DROP TABLE own_document_tax;
CREATE TABLE own_document_tax (
    amount numeric(12,4),
    taxed_amount numeric(12,4),
    own_document bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    tax bigint,
    id bigint NOT NULL DEFAULT nextval('own_document_tax_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX own_document_tax_own_document_idx ON own_document_tax(own_document);
CREATE INDEX own_document_tax_tax_idx ON own_document_tax(tax);
INSERT INTO own_document_tax (amount, taxed_amount, own_document, tax, id) SELECT amount, taxed_amount, own_document, tax, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _relation_types_data;
DROP TABLE _relation_types_data;
CREATE TABLE _relation_types_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    entity_relation int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_relation_types_data_id_seq'),
    relation_type int,
    PRIMARY KEY (id)
);
CREATE INDEX relation_types_entity_relation_idx ON _relation_types_data(entity_relation);
CREATE INDEX relation_types_relation_type_idx ON _relation_types_data(relation_type);
INSERT INTO _relation_types_data (_start_t, _end_t, entity_relation, _table, id, relation_type) SELECT _start_t, _end_t, entity_relation, _table, id, relation_type FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM task_priority;
DROP TABLE task_priority;
CREATE TABLE task_priority (
    id int NOT NULL DEFAULT nextval('task_priority_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO task_priority (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _tax_profile_data;
DROP TABLE _tax_profile_data;
CREATE TABLE _tax_profile_data (
    profile varchar(80),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    tax int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_tax_profile_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX tax_profile_tax_idx ON _tax_profile_data(tax);
INSERT INTO _tax_profile_data (profile, _start_t, _end_t, tax, _table, id) SELECT profile, _start_t, _end_t, tax, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _own_pos_data;
DROP TABLE _own_pos_data;
CREATE TABLE _own_pos_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _state int DEFAULT -1 NOT NULL,
    _table int NOT NULL,
    own_entity int,
    id bigint NOT NULL DEFAULT nextval('_own_pos_data_id_seq'),
    stock_by_invoice boolean,
    number char(4),
    PRIMARY KEY (id)
);
CREATE INDEX own_pos_own_entity_idx ON _own_pos_data(own_entity);
INSERT INTO _own_pos_data (_start_t, _end_t, _table, own_entity, id, stock_by_invoice, number) SELECT _start_t, _end_t, _table, own_entity, id, stock_by_invoice, number FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _own_branch_data;
DROP TABLE _own_branch_data;
CREATE TABLE _own_branch_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    own_entity int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_own_branch_data_id_seq'),
    number char(4),
    PRIMARY KEY (id)
);
CREATE INDEX own_branch_own_entity_idx ON _own_branch_data(own_entity);
INSERT INTO _own_branch_data (_start_t, _end_t, own_entity, _table, id, number) SELECT _start_t, _end_t, own_entity, _table, id, number FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _bank_data;
DROP TABLE _bank_data;
CREATE TABLE _bank_data (
    persona int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_bank_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX bank_persona_idx ON _bank_data(persona);
INSERT INTO _bank_data (persona, _start_t, name, _table, id, _end_t) SELECT persona, _start_t, name, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM physical_structure;
DROP TABLE physical_structure;
CREATE TABLE physical_structure (
    id int NOT NULL DEFAULT nextval('physical_structure_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO physical_structure (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM bank_branch;
DROP TABLE bank_branch;
CREATE TABLE bank_branch (
    id int NOT NULL DEFAULT nextval('bank_branch_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO bank_branch (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_branch;
DROP TABLE own_branch;
CREATE TABLE own_branch (
    id int NOT NULL DEFAULT nextval('own_branch_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO own_branch (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _obligation_type_data;
DROP TABLE _obligation_type_data;
CREATE TABLE _obligation_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    description text,
    id bigint NOT NULL DEFAULT nextval('_obligation_type_data_id_seq'),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _obligation_type_data (_start_t, description, id, _table, account_plan, _end_t, name) SELECT _start_t, description, id, _table, account_plan, _end_t, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_code;
DROP TABLE product_code;
CREATE TABLE product_code (
    id int NOT NULL DEFAULT nextval('product_code_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_code (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM employee_comm_prod_client;
DROP TABLE employee_comm_prod_client;
CREATE TABLE employee_comm_prod_client (
    id bigint NOT NULL DEFAULT nextval('employee_comm_prod_client_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO employee_comm_prod_client (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_provider;
DROP TABLE product_provider;
CREATE TABLE product_provider (
    id bigint NOT NULL DEFAULT nextval('product_provider_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_provider (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM proceeding;
DROP TABLE proceeding;
CREATE TABLE proceeding (
    responsable bigint,
    branch bigint,
    entry bigint,
    date timestamp,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id bigint NOT NULL DEFAULT nextval('proceeding_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX proceeding_branch_idx ON proceeding(branch);
CREATE INDEX proceeding_responsable_idx ON proceeding(responsable);
INSERT INTO proceeding (responsable, branch, entry, date, id) SELECT responsable, branch, entry, date, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_code_data;
DROP TABLE _product_code_data;
CREATE TABLE _product_code_data (
    code varchar(30),
    product int,
    product_code_type int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_code_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX product_code_product_idx ON _product_code_data(product);
CREATE INDEX product_code_product_code_type_idx ON _product_code_data(product_code_type);
INSERT INTO _product_code_data (code, product, product_code_type, _start_t, _end_t, _table, id) SELECT code, product, product_code_type, _start_t, _end_t, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _logical2physical_data;
DROP TABLE _logical2physical_data;
CREATE TABLE _logical2physical_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_logical2physical_data_id_seq'),
    logical int,
    physical int,
    PRIMARY KEY (id)
);
CREATE INDEX logical2physical_physical_idx ON _logical2physical_data(physical);
CREATE INDEX logical2physical_logical_idx ON _logical2physical_data(logical);
INSERT INTO _logical2physical_data (_start_t, _end_t, _table, id, logical, physical) SELECT _start_t, _end_t, _table, id, logical, physical FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_type;
DROP TABLE entity_type;
CREATE TABLE entity_type (
    id int NOT NULL DEFAULT nextval('entity_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_uid;
DROP TABLE entity_uid;
CREATE TABLE entity_uid (
    id int NOT NULL DEFAULT nextval('entity_uid_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_uid (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _price_data;
DROP TABLE _price_data;
CREATE TABLE _price_data (
    currency_type int,
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    excluded boolean,
    price_type int,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_price_data_id_seq'),
    unit_price numeric(12,4),
    PRIMARY KEY (id)
);
CREATE INDEX price_currency_type_idx ON _price_data(currency_type);
CREATE INDEX price_product_idx ON _price_data(product);
CREATE INDEX price_price_type_idx ON _price_data(price_type);
INSERT INTO _price_data (currency_type, product, _start_t, _end_t, excluded, price_type, _table, id, unit_price) SELECT currency_type, product, _start_t, _end_t, excluded, price_type, _table, id, unit_price FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _employee_data;
DROP TABLE _employee_data;
CREATE TABLE _employee_data (
    commission int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    own_entity int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_employee_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX employee_own_entity_idx ON _employee_data(own_entity);
CREATE INDEX employee_commission_idx ON _employee_data(commission);
INSERT INTO _employee_data (commission, _start_t, _end_t, own_entity, _table, id) SELECT commission, _start_t, _end_t, own_entity, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM category_type;
DROP TABLE category_type;
CREATE TABLE category_type (
    id int NOT NULL DEFAULT nextval('category_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO category_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_document_tax;
DROP TABLE alien_document_tax;
CREATE TABLE alien_document_tax (
    amount numeric(12,4),
    taxed_amount numeric(12,4),
    entry bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    tax bigint,
    id bigint NOT NULL DEFAULT nextval('alien_document_tax_id_seq'),
    alien_document bigint,
    PRIMARY KEY (id)
);
CREATE INDEX alien_document_tax_tax_idx ON alien_document_tax(tax);
CREATE INDEX alien_document_tax_alien_document_idx ON alien_document_tax(alien_document);
INSERT INTO alien_document_tax (amount, taxed_amount, entry, tax, id, alien_document) SELECT amount, taxed_amount, entry, tax, id, alien_document FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_structure;
DROP TABLE product_structure;
CREATE TABLE product_structure (
    id bigint NOT NULL DEFAULT nextval('product_structure_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_structure (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM provider;
DROP TABLE provider;
CREATE TABLE provider (
    id int NOT NULL DEFAULT nextval('provider_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO provider (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_item;
DROP TABLE own_item;
CREATE TABLE own_item (
    discount numeric(12,4),
    entry bigint,
    bonus numeric(12,4),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    own_document bigint,
    account_plan bigint,
    id bigint NOT NULL DEFAULT nextval('own_item_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX own_item_own_document_idx ON own_item(own_document);
INSERT INTO own_item (discount, entry, bonus, own_document, account_plan, id) SELECT discount, entry, bonus, own_document, account_plan, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _role_data;
DROP TABLE _role_data;
CREATE TABLE _role_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_role_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _role_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM movement;
DROP TABLE movement;
CREATE TABLE movement (
    amount numeric(12,4),
    proceeding bigint,
    parent bigint,
    movement_type bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id bigint NOT NULL DEFAULT nextval('movement_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX movement_movement_type_idx ON movement(movement_type);
CREATE INDEX movement_proceeding_idx ON movement(proceeding);
CREATE INDEX movement_parent_idx ON movement(parent);
INSERT INTO movement (amount, proceeding, parent, movement_type, id) SELECT amount, proceeding, parent, movement_type, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM third_party_cheque;
DROP TABLE third_party_cheque;
CREATE TABLE third_party_cheque (
    bank_branch bigint,
    entry bigint,
    credit_date timestamp,
    emmited_by varchar(80),
    third_party_cheque_delay int,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id bigint NOT NULL DEFAULT nextval('third_party_cheque_id_seq'),
    account_plan bigint,
    PRIMARY KEY (id)
);
CREATE INDEX third_party_cheque_bank_branch_idx ON third_party_cheque(bank_branch);
INSERT INTO third_party_cheque (bank_branch, entry, credit_date, emmited_by, third_party_cheque_delay, id, account_plan) SELECT bank_branch, entry, credit_date, emmited_by, third_party_cheque_delay, id, account_plan FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _alien_storehouse_data;
DROP TABLE _alien_storehouse_data;
CREATE TABLE _alien_storehouse_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_alien_storehouse_data_id_seq'),
    alien_entity int,
    PRIMARY KEY (id)
);
CREATE INDEX alien_storehouse_alien_entity_idx ON _alien_storehouse_data(alien_entity);
INSERT INTO _alien_storehouse_data (_start_t, _end_t, _table, id, alien_entity) SELECT _start_t, _end_t, _table, id, alien_entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_attribute;
DROP TABLE entity_attribute;
CREATE TABLE entity_attribute (
    id int NOT NULL DEFAULT nextval('entity_attribute_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_attribute (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _persona_tax_profile_data;
DROP TABLE _persona_tax_profile_data;
CREATE TABLE _persona_tax_profile_data (
    tax_profile int,
    persona int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_persona_tax_profile_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX persona_tax_profile_persona_idx ON _persona_tax_profile_data(persona);
CREATE INDEX persona_tax_profile_tax_profile_idx ON _persona_tax_profile_data(tax_profile);
INSERT INTO _persona_tax_profile_data (tax_profile, persona, _start_t, _end_t, _table, id) SELECT tax_profile, persona, _start_t, _end_t, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_document;
DROP TABLE own_document;
CREATE TABLE own_document (
    due_date timestamp,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    own_document_type bigint,
    related_to varchar(255),
    number char(30),
    is_void boolean,
    salesperson bigint,
    address varchar(255),
    date timestamp,
    authorized_amount numeric(12,4),
    recipient bigint,
    id bigint NOT NULL DEFAULT nextval('own_document_id_seq'),
    transport bigint,
    end_timestamp timestamp,
    tax_total numeric(12,4),
    start_timestamp timestamp,
    exempt_net_total numeric(12,4),
    own_entity bigint,
    bundles int,
    net_total numeric(12,4),
    observations text,
    discount_percent numeric(12,4),
    authorized_by bigint,
    entry bigint,
    guide_number char(30),
    PRIMARY KEY (id)
);
CREATE INDEX own_document_authorized_by_idx ON own_document(authorized_by);
CREATE INDEX own_document_own_document_type_idx ON own_document(own_document_type);
CREATE INDEX own_document_recipient_idx ON own_document(recipient);
CREATE INDEX own_document_salesperson_idx ON own_document(salesperson);
CREATE INDEX own_document_transport_idx ON own_document(transport);
CREATE INDEX own_document_own_entity_idx ON own_document(own_entity);
INSERT INTO own_document (due_date, own_document_type, related_to, number, is_void, salesperson, address, date, authorized_amount, recipient, id, transport, end_timestamp, tax_total, start_timestamp, exempt_net_total, own_entity, bundles, net_total, observations, discount_percent, authorized_by, entry, guide_number) SELECT due_date, own_document_type, related_to, number, is_void, salesperson, address, date, authorized_amount, recipient, id, transport, end_timestamp, tax_total, start_timestamp, exempt_net_total, own_entity, bundles, net_total, observations, discount_percent, authorized_by, entry, guide_number FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _employee_comm_prod_client_data;
DROP TABLE _employee_comm_prod_client_data;
CREATE TABLE _employee_comm_prod_client_data (
    commission int,
    product int,
    client int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    employee int,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_employee_comm_prod_client_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX employee_comm_prod_client_product_idx ON _employee_comm_prod_client_data(product);
CREATE INDEX employee_comm_prod_client_client_idx ON _employee_comm_prod_client_data(client);
CREATE INDEX employee_comm_prod_client_employee_idx ON _employee_comm_prod_client_data(employee);
CREATE INDEX employee_comm_prod_client_commission_idx ON _employee_comm_prod_client_data(commission);
INSERT INTO _employee_comm_prod_client_data (commission, product, client, _start_t, _end_t, employee, _table, id) SELECT commission, product, client, _start_t, _end_t, employee, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_product_stock_item;
DROP TABLE own_product_stock_item;
CREATE TABLE own_product_stock_item (
    own_item bigint,
    observations text,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    difference numeric(12,4),
    product bigint,
    id bigint NOT NULL DEFAULT nextval('own_product_stock_item_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX own_product_stock_item_own_item_idx ON own_product_stock_item(own_item);
CREATE INDEX own_product_stock_item_product_idx ON own_product_stock_item(product);
INSERT INTO own_product_stock_item (own_item, observations, difference, product, id) SELECT own_item, observations, difference, product, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM erm_log;
DROP TABLE erm_log;
CREATE TABLE erm_log (
    text text,
    id int NOT NULL DEFAULT nextval('erm_log_id_seq'),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    erm int,
    PRIMARY KEY (id)
);
CREATE INDEX erm_log_erm_idx ON erm_log(erm);
INSERT INTO erm_log (text, id, erm) SELECT text, id, erm FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM address;
DROP TABLE address;
CREATE TABLE address (
    id int NOT NULL DEFAULT nextval('address_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO address (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _task_priority_data;
DROP TABLE _task_priority_data;
CREATE TABLE _task_priority_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_task_priority_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _task_priority_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM obligation;
DROP TABLE obligation;
CREATE TABLE obligation (
    obligation_type bigint,
    amount numeric(12,4),
    date timestamp,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    account_plan bigint,
    id int NOT NULL DEFAULT nextval('obligation_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX obligation_obligation_type_idx ON obligation(obligation_type);
INSERT INTO obligation (obligation_type, amount, date, account_plan, id) SELECT obligation_type, amount, date, account_plan, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_storehouse;
DROP TABLE own_storehouse;
CREATE TABLE own_storehouse (
    id int NOT NULL DEFAULT nextval('own_storehouse_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO own_storehouse (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_document;
DROP TABLE alien_document;
CREATE TABLE alien_document (
    due_date timestamp,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    number char(30),
    date timestamp,
    is_void boolean,
    salesperson bigint,
    received_by bigint,
    own_entity bigint,
    id bigint NOT NULL DEFAULT nextval('alien_document_id_seq'),
    transport bigint,
    alien_document_type bigint,
    tax_total numeric(12,4),
    start_timestamp timestamp,
    exempt_net_total numeric(12,4),
    sender bigint,
    bundles int,
    net_total numeric(12,4),
    observations text,
    discount_percent numeric(12,4),
    entry bigint,
    end_timestamp timestamp,
    guide_number char(30),
    PRIMARY KEY (id)
);
CREATE INDEX alien_document_transport_idx ON alien_document(transport);
CREATE INDEX alien_document_sender_idx ON alien_document(sender);
CREATE INDEX alien_document_own_entity_idx ON alien_document(own_entity);
CREATE INDEX alien_document_alien_document_type_idx ON alien_document(alien_document_type);
CREATE INDEX alien_document_received_by_idx ON alien_document(received_by);
CREATE INDEX alien_document_salesperson_idx ON alien_document(salesperson);
INSERT INTO alien_document (due_date, number, date, is_void, salesperson, received_by, own_entity, id, transport, alien_document_type, tax_total, start_timestamp, exempt_net_total, sender, bundles, net_total, observations, discount_percent, entry, end_timestamp, guide_number) SELECT due_date, number, date, is_void, salesperson, received_by, own_entity, id, transport, alien_document_type, tax_total, start_timestamp, exempt_net_total, sender, bundles, net_total, observations, discount_percent, entry, end_timestamp, guide_number FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_tax_data;
DROP TABLE _product_tax_data;
CREATE TABLE _product_tax_data (
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    entry bigint,
    _state int DEFAULT -1 NOT NULL,
    _table bigint NOT NULL,
    tax int,
    amount numeric(12,4),
    id bigint NOT NULL DEFAULT nextval('_product_tax_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX product_tax_product_idx ON _product_tax_data(product);
CREATE INDEX product_tax_tax_idx ON _product_tax_data(tax);
INSERT INTO _product_tax_data (product, _start_t, _end_t, entry, _table, tax, amount, id) SELECT product, _start_t, _end_t, entry, _table, tax, amount, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM price_type;
DROP TABLE price_type;
CREATE TABLE price_type (
    id int NOT NULL DEFAULT nextval('price_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO price_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM country;
DROP TABLE country;
CREATE TABLE country (
    id int NOT NULL DEFAULT nextval('country_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO country (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _bank_branch_data;
DROP TABLE _bank_branch_data;
CREATE TABLE _bank_branch_data (
    persona int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_bank_branch_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX bank_branch_persona_idx ON _bank_branch_data(persona);
INSERT INTO _bank_branch_data (persona, _start_t, _end_t, _table, id) SELECT persona, _start_t, _end_t, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _address_data;
DROP TABLE _address_data;
CREATE TABLE _address_data (
    comment varchar(80),
    _table int NOT NULL,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    address varchar(255),
    address_type int,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_address_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX address_entity_idx ON _address_data(entity);
CREATE INDEX address_address_type_idx ON _address_data(address_type);
INSERT INTO _address_data (comment, _table, _start_t, _end_t, address, address_type, id, entity) SELECT comment, _table, _start_t, _end_t, address, address_type, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM credit_card_plan;
DROP TABLE credit_card_plan;
CREATE TABLE credit_card_plan (
    id int NOT NULL DEFAULT nextval('credit_card_plan_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO credit_card_plan (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_measure_type;
DROP TABLE product_measure_type;
CREATE TABLE product_measure_type (
    id int NOT NULL DEFAULT nextval('product_measure_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_measure_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _relation_type_data;
DROP TABLE _relation_type_data;
CREATE TABLE _relation_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_relation_type_data_id_seq'),
    name varchar(255),
    PRIMARY KEY (id)
);
INSERT INTO _relation_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _physical_structure_data;
DROP TABLE _physical_structure_data;
CREATE TABLE _physical_structure_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    parent int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_physical_structure_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX physical_structure_parent_idx ON _physical_structure_data(parent);
INSERT INTO _physical_structure_data (_start_t, name, parent, _table, id, _end_t) SELECT _start_t, name, parent, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _logical_structure_data;
DROP TABLE _logical_structure_data;
CREATE TABLE _logical_structure_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    parent int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_logical_structure_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX logical_structure_parent_idx ON _logical_structure_data(parent);
INSERT INTO _logical_structure_data (_start_t, name, parent, _table, id, _end_t) SELECT _start_t, name, parent, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM task_status;
DROP TABLE task_status;
CREATE TABLE task_status (
    id int NOT NULL DEFAULT nextval('task_status_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO task_status (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_storehouse;
DROP TABLE alien_storehouse;
CREATE TABLE alien_storehouse (
    id int NOT NULL DEFAULT nextval('alien_storehouse_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO alien_storehouse (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _currency_type_data;
DROP TABLE _currency_type_data;
CREATE TABLE _currency_type_data (
    rate numeric(10,2),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(50),
    format varchar(50),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    id bigint NOT NULL DEFAULT nextval('_currency_type_data_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO _currency_type_data (rate, _start_t, name, format, _table, account_plan, _end_t, id) SELECT rate, _start_t, name, format, _table, account_plan, _end_t, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _country_data;
DROP TABLE _country_data;
CREATE TABLE _country_data (
    phone integer,
    iso3 char(3),
    iso2 char(2),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _table int NOT NULL,
    un integer,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_country_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
INSERT INTO _country_data (phone, iso3, iso2, _start_t, name, _table, un, id, _end_t) SELECT phone, iso3, iso2, _start_t, name, _table, un, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM employee_role;
DROP TABLE employee_role;
CREATE TABLE employee_role (
    id int NOT NULL DEFAULT nextval('employee_role_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO employee_role (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM ticket_type;
DROP TABLE ticket_type;
CREATE TABLE ticket_type (
    id int NOT NULL DEFAULT nextval('ticket_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO ticket_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM employee_comm_prod;
DROP TABLE employee_comm_prod;
CREATE TABLE employee_comm_prod (
    id bigint NOT NULL DEFAULT nextval('employee_comm_prod_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO employee_comm_prod (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM cheque;
DROP TABLE cheque;
CREATE TABLE cheque (
    chequebook int,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    date timestamp,
    entry bigint,
    account_plan bigint,
    id bigint NOT NULL DEFAULT nextval('cheque_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX cheque_chequebook_idx ON cheque(chequebook);
INSERT INTO cheque (chequebook, date, entry, account_plan, id) SELECT chequebook, date, entry, account_plan, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_pos_document;
DROP TABLE own_pos_document;
CREATE TABLE own_pos_document (
    id int NOT NULL DEFAULT nextval('own_pos_document_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO own_pos_document (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM currency_type;
DROP TABLE currency_type;
CREATE TABLE currency_type (
    id int NOT NULL DEFAULT nextval('currency_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO currency_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM bank_account_movement;
DROP TABLE bank_account_movement;
CREATE TABLE bank_account_movement (
    origin bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    bank_account_movement_delay int,
    destination bigint,
    id bigint NOT NULL DEFAULT nextval('bank_account_movement_id_seq'),
    movement bigint,
    PRIMARY KEY (id)
);
CREATE INDEX bank_account_movement_movement_idx ON bank_account_movement(movement);
CREATE INDEX bank_account_movement_destination_idx ON bank_account_movement(destination);
CREATE INDEX bank_account_movement_origin_idx ON bank_account_movement(origin);
INSERT INTO bank_account_movement (origin, bank_account_movement_delay, destination, id, movement) SELECT origin, bank_account_movement_delay, destination, id, movement FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _task_status_data;
DROP TABLE _task_status_data;
CREATE TABLE _task_status_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_task_status_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _task_status_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _file_data;
DROP TABLE _file_data;
CREATE TABLE _file_data (
    mimetype char(80),
    application char(15),
    _table int NOT NULL,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    checksum char(32),
    filename varchar(255),
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_file_data_id_seq'),
    size int,
    PRIMARY KEY (id)
);
INSERT INTO _file_data (mimetype, application, _table, _start_t, _end_t, checksum, filename, id, size) SELECT mimetype, application, _table, _start_t, _end_t, checksum, filename, id, size FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_item;
DROP TABLE alien_item;
CREATE TABLE alien_item (
    discount numeric(12,4),
    alien_document bigint,
    entry bigint,
    bonus numeric(12,4),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    account_plan bigint,
    id bigint NOT NULL DEFAULT nextval('alien_item_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX alien_item_alien_document_idx ON alien_item(alien_document);
INSERT INTO alien_item (discount, alien_document, entry, bonus, account_plan, id) SELECT discount, alien_document, entry, bonus, account_plan, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_item_tax;
DROP TABLE alien_item_tax;
CREATE TABLE alien_item_tax (
    amount numeric(12,4),
    entry bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    tax bigint,
    id bigint NOT NULL DEFAULT nextval('alien_item_tax_id_seq'),
    alien_item bigint,
    PRIMARY KEY (id)
);
CREATE INDEX alien_item_tax_tax_idx ON alien_item_tax(tax);
CREATE INDEX alien_item_tax_alien_item_idx ON alien_item_tax(alien_item);
INSERT INTO alien_item_tax (amount, entry, tax, id, alien_item) SELECT amount, entry, tax, id, alien_item FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _credit_limit_data;
DROP TABLE _credit_limit_data;
CREATE TABLE _credit_limit_data (
    credit_limit numeric(12,4),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    movement_type int,
    _table int NOT NULL,
    time_stamp timestamp,
    balance numeric(12,4),
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_credit_limit_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX credit_limit_entity_idx ON _credit_limit_data(entity);
CREATE INDEX credit_limit_movement_type_idx ON _credit_limit_data(movement_type);
INSERT INTO _credit_limit_data (credit_limit, _start_t, _end_t, movement_type, _table, time_stamp, balance, id, entity) SELECT credit_limit, _start_t, _end_t, movement_type, _table, time_stamp, balance, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM account_plan;
DROP TABLE account_plan;
CREATE TABLE account_plan (
    id int NOT NULL DEFAULT nextval('account_plan_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO account_plan (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _ticket_data;
DROP TABLE _ticket_data;
CREATE TABLE _ticket_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    ticket_type int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_ticket_data_id_seq'),
    account_plan int,
    PRIMARY KEY (id)
);
CREATE INDEX ticket_ticket_type_idx ON _ticket_data(ticket_type);
INSERT INTO _ticket_data (_start_t, _end_t, ticket_type, _table, id, account_plan) SELECT _start_t, _end_t, ticket_type, _table, id, account_plan FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM commission;
DROP TABLE commission;
CREATE TABLE commission (
    id int NOT NULL DEFAULT nextval('commission_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO commission (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM sys_account2account_plan;
DROP TABLE sys_account2account_plan;
CREATE TABLE sys_account2account_plan (
    id int NOT NULL DEFAULT nextval('sys_account2account_plan_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO sys_account2account_plan (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM document_relation_type;
DROP TABLE document_relation_type;
CREATE TABLE document_relation_type (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id int NOT NULL DEFAULT nextval('document_relation_type_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO document_relation_type (id, name) SELECT id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_data;
DROP TABLE _entity_data;
CREATE TABLE _entity_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(255),
    is_active boolean,
    start_time timestamp,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    anniversary date,
    observations text,
    _table int NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
INSERT INTO _entity_data (_start_t, name, is_active, start_time, account_plan, anniversary, observations, _table, id, _end_t) SELECT _start_t, name, is_active, start_time, account_plan, anniversary, observations, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM currency_movement;
DROP TABLE currency_movement;
CREATE TABLE currency_movement (
    currency_type bigint,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('currency_movement_id_seq'),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    movement bigint,
    PRIMARY KEY (id)
);
CREATE INDEX currency_movement_currency_type_idx ON currency_movement(currency_type);
CREATE INDEX currency_movement_movement_idx ON currency_movement(movement);
INSERT INTO currency_movement (currency_type, entry, id, movement) SELECT currency_type, entry, id, movement FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _own_entity_data;
DROP TABLE _own_entity_data;
CREATE TABLE _own_entity_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_own_entity_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX own_entity_entity_idx ON _own_entity_data(entity);
INSERT INTO _own_entity_data (_start_t, _end_t, _table, id, entity) SELECT _start_t, _end_t, _table, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM comm_prod_client;
DROP TABLE comm_prod_client;
CREATE TABLE comm_prod_client (
    id bigint NOT NULL DEFAULT nextval('comm_prod_client_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO comm_prod_client (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM cheque_tax;
DROP TABLE cheque_tax;
CREATE TABLE cheque_tax (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    tax bigint,
    id bigint NOT NULL DEFAULT nextval('cheque_tax_id_seq'),
    cheque bigint,
    PRIMARY KEY (id)
);
CREATE INDEX cheque_tax_cheque_idx ON cheque_tax(cheque);
CREATE INDEX cheque_tax_tax_idx ON cheque_tax(tax);
INSERT INTO cheque_tax (tax, id, cheque) SELECT tax, id, cheque FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _own_storehouse_data;
DROP TABLE _own_storehouse_data;
CREATE TABLE _own_storehouse_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    own_entity int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_own_storehouse_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX own_storehouse_own_entity_idx ON _own_storehouse_data(own_entity);
INSERT INTO _own_storehouse_data (_start_t, _end_t, own_entity, _table, id) SELECT _start_t, _end_t, own_entity, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM barter_type;
DROP TABLE barter_type;
CREATE TABLE barter_type (
    id int NOT NULL DEFAULT nextval('barter_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO barter_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_store_data;
DROP TABLE _product_store_data;
CREATE TABLE _product_store_data (
    hiwater numeric(12,4),
    product int,
    lowater numeric(12,4),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    storehouse int,
    _table bigint NOT NULL,
    level numeric(12,4),
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_store_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX product_store_product_idx ON _product_store_data(product);
CREATE INDEX product_store_storehouse_idx ON _product_store_data(storehouse);
INSERT INTO _product_store_data (hiwater, product, lowater, _start_t, _end_t, storehouse, _table, level, id) SELECT hiwater, product, lowater, _start_t, _end_t, storehouse, _table, level, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM barter_movement;
DROP TABLE barter_movement;
CREATE TABLE barter_movement (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('barter_movement_id_seq'),
    barter bigint,
    movement bigint,
    PRIMARY KEY (id)
);
CREATE INDEX barter_movement_barter_idx ON barter_movement(barter);
CREATE INDEX barter_movement_movement_idx ON barter_movement(movement);
INSERT INTO barter_movement (entry, id, barter, movement) SELECT entry, id, barter, movement FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _ticket_type_data;
DROP TABLE _ticket_type_data;
CREATE TABLE _ticket_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    description text,
    id bigint NOT NULL DEFAULT nextval('_ticket_type_data_id_seq'),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _ticket_type_data (_start_t, description, id, _table, account_plan, _end_t, name) SELECT _start_t, description, id, _table, account_plan, _end_t, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM obligation_type;
DROP TABLE obligation_type;
CREATE TABLE obligation_type (
    id int NOT NULL DEFAULT nextval('obligation_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO obligation_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM tax_profile;
DROP TABLE tax_profile;
CREATE TABLE tax_profile (
    id int NOT NULL DEFAULT nextval('tax_profile_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO tax_profile (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM credit_limit;
DROP TABLE credit_limit;
CREATE TABLE credit_limit (
    id int NOT NULL DEFAULT nextval('credit_limit_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO credit_limit (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _provider_data;
DROP TABLE _provider_data;
CREATE TABLE _provider_data (
    persona int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_provider_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX provider_persona_idx ON _provider_data(persona);
INSERT INTO _provider_data (persona, _start_t, _end_t, _table, id) SELECT persona, _start_t, _end_t, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM employee_comm_client;
DROP TABLE employee_comm_client;
CREATE TABLE employee_comm_client (
    id bigint NOT NULL DEFAULT nextval('employee_comm_client_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO employee_comm_client (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_product_item;
DROP TABLE own_product_item;
CREATE TABLE own_product_item (
    own_item bigint,
    account_plan bigint,
    entry bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    price bigint,
    id bigint NOT NULL DEFAULT nextval('own_product_item_id_seq'),
    unit_price numeric(12,4),
    qty numeric(12,4),
    PRIMARY KEY (id)
);
CREATE INDEX own_product_item_own_item_idx ON own_product_item(own_item);
CREATE INDEX own_product_item_price_idx ON own_product_item(price);
INSERT INTO own_product_item (own_item, account_plan, entry, price, id, unit_price, qty) SELECT own_item, account_plan, entry, price, id, unit_price, qty FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM logical2physical;
DROP TABLE logical2physical;
CREATE TABLE logical2physical (
    id int NOT NULL DEFAULT nextval('logical2physical_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO logical2physical (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM organization;
DROP TABLE organization;
CREATE TABLE organization (
    id int NOT NULL DEFAULT nextval('organization_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO organization (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _barter_data;
DROP TABLE _barter_data;
CREATE TABLE _barter_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _state int DEFAULT -1 NOT NULL,
    _table int NOT NULL,
    barter_type int,
    account_plan int,
    _end_t timestamp,
    id bigint NOT NULL DEFAULT nextval('_barter_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX barter_barter_type_idx ON _barter_data(barter_type);
INSERT INTO _barter_data (_start_t, name, _table, barter_type, account_plan, _end_t, id) SELECT _start_t, name, _table, barter_type, account_plan, _end_t, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_relation;
DROP TABLE entity_relation;
CREATE TABLE entity_relation (
    id int NOT NULL DEFAULT nextval('entity_relation_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_relation (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _price_type_data;
DROP TABLE _price_type_data;
CREATE TABLE _price_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    parent int,
    is_active boolean,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_price_type_data_id_seq'),
    branch int,
    formula text,
    end_date timestamp,
    _table int NOT NULL,
    start_date timestamp,
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX price_type_branch_idx ON _price_type_data(branch);
CREATE INDEX price_type_parent_idx ON _price_type_data(parent);
INSERT INTO _price_type_data (_start_t, name, parent, is_active, id, branch, formula, end_date, _table, start_date, _end_t) SELECT _start_t, name, parent, is_active, id, branch, formula, end_date, _table, start_date, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM obligation_movement;
DROP TABLE obligation_movement;
CREATE TABLE obligation_movement (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    obligation int,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('obligation_movement_id_seq'),
    movement bigint,
    PRIMARY KEY (id)
);
CREATE INDEX obligation_movement_movement_idx ON obligation_movement(movement);
CREATE INDEX obligation_movement_obligation_idx ON obligation_movement(obligation);
INSERT INTO obligation_movement (obligation, entry, id, movement) SELECT obligation, entry, id, movement FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _organization_data;
DROP TABLE _organization_data;
CREATE TABLE _organization_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    own_entity int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_organization_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX organization_own_entity_idx ON _organization_data(own_entity);
INSERT INTO _organization_data (_start_t, _end_t, own_entity, _table, id) SELECT _start_t, _end_t, own_entity, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM category_attribute;
DROP TABLE category_attribute;
CREATE TABLE category_attribute (
    id int NOT NULL DEFAULT nextval('category_attribute_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO category_attribute (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM erm;
DROP TABLE erm;
CREATE TABLE erm (
    erm_type bigint,
    description varchar(80),
    time timestamp,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    made_by bigint,
    id int NOT NULL DEFAULT nextval('erm_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX erm_made_by_idx ON erm(made_by);
CREATE INDEX erm_erm_type_idx ON erm(erm_type);
INSERT INTO erm (erm_type, description, time, made_by, id) SELECT erm_type, description, time, made_by, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_types;
DROP TABLE entity_types;
CREATE TABLE entity_types (
    id int NOT NULL DEFAULT nextval('entity_types_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_types (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_uid_type_data;
DROP TABLE _entity_uid_type_data;
CREATE TABLE _entity_uid_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_uid_type_data_id_seq'),
    name varchar(255),
    PRIMARY KEY (id)
);
INSERT INTO _entity_uid_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _comm_prod_client_data;
DROP TABLE _comm_prod_client_data;
CREATE TABLE _comm_prod_client_data (
    commission int,
    product int,
    client int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_comm_prod_client_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX comm_prod_client_product_idx ON _comm_prod_client_data(product);
CREATE INDEX comm_prod_client_client_idx ON _comm_prod_client_data(client);
CREATE INDEX comm_prod_client_commission_idx ON _comm_prod_client_data(commission);
INSERT INTO _comm_prod_client_data (commission, product, client, _start_t, _end_t, _table, id) SELECT commission, product, client, _start_t, _end_t, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM ticket_movement;
DROP TABLE ticket_movement;
CREATE TABLE ticket_movement (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    ticket bigint,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('ticket_movement_id_seq'),
    movement bigint,
    PRIMARY KEY (id)
);
CREATE INDEX ticket_movement_movement_idx ON ticket_movement(movement);
CREATE INDEX ticket_movement_ticket_idx ON ticket_movement(ticket);
INSERT INTO ticket_movement (ticket, entry, id, movement) SELECT ticket, entry, id, movement FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_code_type_data;
DROP TABLE _product_code_type_data;
CREATE TABLE _product_code_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_code_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _product_code_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM erm_file;
DROP TABLE erm_file;
CREATE TABLE erm_file (
    erm int,
    id int NOT NULL DEFAULT nextval('erm_file_id_seq'),
    file bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    PRIMARY KEY (id)
);
CREATE INDEX erm_file_file_idx ON erm_file(file);
CREATE INDEX erm_file_erm_idx ON erm_file(erm);
INSERT INTO erm_file (erm, id, file) SELECT erm, id, file FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _task_type_data;
DROP TABLE _task_type_data;
CREATE TABLE _task_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_task_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _task_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM third_party_cheque_movement;
DROP TABLE third_party_cheque_movement;
CREATE TABLE third_party_cheque_movement (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    third_party_cheque bigint,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('third_party_cheque_movement_id_seq'),
    movement bigint,
    PRIMARY KEY (id)
);
CREATE INDEX third_party_cheque_movement_third_party_cheque_idx ON third_party_cheque_movement(third_party_cheque);
CREATE INDEX third_party_cheque_movement_movement_idx ON third_party_cheque_movement(movement);
INSERT INTO third_party_cheque_movement (third_party_cheque, entry, id, movement) SELECT third_party_cheque, entry, id, movement FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM tax;
DROP TABLE tax;
CREATE TABLE tax (
    id int NOT NULL DEFAULT nextval('tax_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO tax (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM erm_entity;
DROP TABLE erm_entity;
CREATE TABLE erm_entity (
    erm int,
    id int NOT NULL DEFAULT nextval('erm_entity_id_seq'),
    made_to bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    PRIMARY KEY (id)
);
CREATE INDEX erm_entity_erm_idx ON erm_entity(erm);
CREATE INDEX erm_entity_made_to_idx ON erm_entity(made_to);
INSERT INTO erm_entity (erm, id, made_to) SELECT erm, id, made_to FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _contact_data;
DROP TABLE _contact_data;
CREATE TABLE _contact_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_contact_data_id_seq'),
    alien_entity int,
    PRIMARY KEY (id)
);
CREATE INDEX contact_alien_entity_idx ON _contact_data(alien_entity);
INSERT INTO _contact_data (_start_t, _end_t, _table, id, alien_entity) SELECT _start_t, _end_t, _table, id, alien_entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_types_data;
DROP TABLE _entity_types_data;
CREATE TABLE _entity_types_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    entity_type int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_types_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX entity_types_entity_type_idx ON _entity_types_data(entity_type);
CREATE INDEX entity_types_entity_idx ON _entity_types_data(entity);
INSERT INTO _entity_types_data (_start_t, _end_t, entity_type, _table, id, entity) SELECT _start_t, _end_t, entity_type, _table, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _own_pos_document_data;
DROP TABLE _own_pos_document_data;
CREATE TABLE _own_pos_document_data (
    document_type int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    print_command varchar(255),
    _state int DEFAULT -1 NOT NULL,
    _table int NOT NULL,
    own_pos int,
    id bigint NOT NULL DEFAULT nextval('_own_pos_document_data_id_seq'),
    number char(30),
    PRIMARY KEY (id)
);
CREATE INDEX own_pos_document_document_type_idx ON _own_pos_document_data(document_type);
CREATE INDEX own_pos_document_own_pos_idx ON _own_pos_document_data(own_pos);
INSERT INTO _own_pos_document_data (document_type, _start_t, _end_t, print_command, _table, own_pos, id, number) SELECT document_type, _start_t, _end_t, print_command, _table, own_pos, id, number FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM __papo__;
DROP TABLE __papo__;
CREATE TABLE __papo__ (
    date timestamp DEFAULT now(),
    value varchar(255),
    key varchar(255),
    PRIMARY KEY (key)
);
INSERT INTO __papo__ (date, value, key) SELECT date, value, key FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM file;
DROP TABLE file;
CREATE TABLE file (
    id int NOT NULL DEFAULT nextval('file_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO file (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_name_type_data;
DROP TABLE _product_name_type_data;
CREATE TABLE _product_name_type_data (
    is_required boolean,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_name_type_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
INSERT INTO _product_name_type_data (is_required, _start_t, name, _table, id, _end_t) SELECT is_required, _start_t, name, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_entity;
DROP TABLE own_entity;
CREATE TABLE own_entity (
    id int NOT NULL DEFAULT nextval('own_entity_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO own_entity (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _credit_card_plan_data;
DROP TABLE _credit_card_plan_data;
CREATE TABLE _credit_card_plan_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    financial_charge numeric(10,2),
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    credit_card int,
    payments int,
    _table int NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_credit_card_plan_data_id_seq'),
    _end_t timestamp,
    surcharge numeric(10,2),
    PRIMARY KEY (id)
);
CREATE INDEX credit_card_plan_credit_card_idx ON _credit_card_plan_data(credit_card);
INSERT INTO _credit_card_plan_data (_start_t, name, financial_charge, account_plan, credit_card, payments, _table, id, _end_t, surcharge) SELECT _start_t, name, financial_charge, account_plan, credit_card, payments, _table, id, _end_t, surcharge FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_structure_data;
DROP TABLE _product_structure_data;
CREATE TABLE _product_structure_data (
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _state int DEFAULT -1 NOT NULL,
    _table bigint NOT NULL,
    component int,
    id bigint NOT NULL DEFAULT nextval('_product_structure_data_id_seq'),
    qty numeric(12,4),
    PRIMARY KEY (id)
);
CREATE INDEX product_structure_component_idx ON _product_structure_data(component);
CREATE INDEX product_structure_product_idx ON _product_structure_data(product);
INSERT INTO _product_structure_data (product, _start_t, _end_t, _table, component, id, qty) SELECT product, _start_t, _end_t, _table, component, id, qty FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_doc4alien_doc;
DROP TABLE alien_doc4alien_doc;
CREATE TABLE alien_doc4alien_doc (
    document_to bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id int NOT NULL DEFAULT nextval('alien_doc4alien_doc_id_seq'),
    relation_type int,
    document_from bigint,
    PRIMARY KEY (id)
);
CREATE INDEX alien_doc4alien_doc_document_from_idx ON alien_doc4alien_doc(document_from);
CREATE INDEX alien_doc4alien_doc_document_to_idx ON alien_doc4alien_doc(document_to);
CREATE INDEX alien_doc4alien_doc_relation_type_idx ON alien_doc4alien_doc(relation_type);
INSERT INTO alien_doc4alien_doc (document_to, id, relation_type, document_from) SELECT document_to, id, relation_type, document_from FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM alien_entity;
DROP TABLE alien_entity;
CREATE TABLE alien_entity (
    id int NOT NULL DEFAULT nextval('alien_entity_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO alien_entity (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _employee_role_data;
DROP TABLE _employee_role_data;
CREATE TABLE _employee_role_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    employee int,
    role int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_employee_role_data_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX employee_role_role_idx ON _employee_role_data(role);
CREATE INDEX employee_role_employee_idx ON _employee_role_data(employee);
INSERT INTO _employee_role_data (_start_t, _end_t, employee, role, _table, id) SELECT _start_t, _end_t, employee, role, _table, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM __state__;
DROP TABLE __state__;
CREATE TABLE __state__ (
    state varchar(40) NOT NULL,
    id int NOT NULL DEFAULT nextval('__state___id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO __state__ (state, id) SELECT state, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _erm_type_data;
DROP TABLE _erm_type_data;
CREATE TABLE _erm_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_erm_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _erm_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _sys_account2account_plan_data;
DROP TABLE _sys_account2account_plan_data;
CREATE TABLE _sys_account2account_plan_data (
    account int,
    code varchar(40),
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(40) NOT NULL,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_sys_account2account_plan_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX account_plan_account_idx ON _sys_account2account_plan_data(account);
INSERT INTO _sys_account2account_plan_data (account, code, _start_t, name, _table, id, _end_t) SELECT account, code, _start_t, name, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM client;
DROP TABLE client;
CREATE TABLE client (
    id int NOT NULL DEFAULT nextval('client_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO client (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM role;
DROP TABLE role;
CREATE TABLE role (
    id int NOT NULL DEFAULT nextval('role_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO role (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_name_type;
DROP TABLE product_name_type;
CREATE TABLE product_name_type (
    id int NOT NULL DEFAULT nextval('product_name_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_name_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _address_type_data;
DROP TABLE _address_type_data;
CREATE TABLE _address_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_address_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _address_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_name;
DROP TABLE product_name;
CREATE TABLE product_name (
    id int NOT NULL DEFAULT nextval('product_name_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_name (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _persona_data;
DROP TABLE _persona_data;
CREATE TABLE _persona_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_persona_data_id_seq'),
    alien_entity int,
    PRIMARY KEY (id)
);
CREATE INDEX persona_alien_entity_idx ON _persona_data(alien_entity);
INSERT INTO _persona_data (_start_t, _end_t, _table, id, alien_entity) SELECT _start_t, _end_t, _table, id, alien_entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product;
DROP TABLE product;
CREATE TABLE product (
    id int NOT NULL DEFAULT nextval('product_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM persona;
DROP TABLE persona;
CREATE TABLE persona (
    id int NOT NULL DEFAULT nextval('persona_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO persona (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_currency_type_data;
DROP TABLE _entity_currency_type_data;
CREATE TABLE _entity_currency_type_data (
    currency_type int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_currency_type_data_id_seq'),
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX entity_currency_type_entity_idx ON _entity_currency_type_data(entity);
CREATE INDEX entity_currency_type_currency_type_idx ON _entity_currency_type_data(currency_type);
INSERT INTO _entity_currency_type_data (currency_type, _start_t, _end_t, _table, id, entity) SELECT currency_type, _start_t, _end_t, _table, id, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_classification_data;
DROP TABLE _product_classification_data;
CREATE TABLE _product_classification_data (
    _end_t timestamp,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(255),
    parent int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_classification_data_id_seq'),
    is_product boolean,
    description text,
    PRIMARY KEY (id)
);
CREATE INDEX product_classification_parent_idx ON _product_classification_data(parent);
INSERT INTO _product_classification_data (_end_t, _start_t, name, parent, _table, id, is_product, description) SELECT _end_t, _start_t, name, parent, _table, id, is_product, description FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _entity_uid_data;
DROP TABLE _entity_uid_data;
CREATE TABLE _entity_uid_data (
    _end_t timestamp,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    uid varchar(50),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_entity_uid_data_id_seq'),
    entity_uid_type int,
    entity int,
    PRIMARY KEY (id)
);
CREATE INDEX entity_uid_entity_idx ON _entity_uid_data(entity);
CREATE INDEX entity_uid_entity_uid_type_idx ON _entity_uid_data(entity_uid_type);
INSERT INTO _entity_uid_data (_end_t, _start_t, uid, _table, id, entity_uid_type, entity) SELECT _end_t, _start_t, uid, _table, id, entity_uid_type, entity FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM price;
DROP TABLE price;
CREATE TABLE price (
    id bigint NOT NULL DEFAULT nextval('price_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO price (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _commission_data;
DROP TABLE _commission_data;
CREATE TABLE _commission_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_commission_data_id_seq'),
    rule text,
    _end_t timestamp,
    PRIMARY KEY (id)
);
INSERT INTO _commission_data (_start_t, name, _table, id, rule, _end_t) SELECT _start_t, name, _table, id, rule, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _client_data;
DROP TABLE _client_data;
CREATE TABLE _client_data (
    commission int,
    score int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    price_type int,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_client_data_id_seq'),
    persona int,
    PRIMARY KEY (id)
);
CREATE INDEX client_commission_idx ON _client_data(commission);
CREATE INDEX client_price_type_idx ON _client_data(price_type);
CREATE INDEX client_persona_idx ON _client_data(persona);
INSERT INTO _client_data (commission, score, _start_t, _end_t, price_type, _table, id, persona) SELECT commission, score, _start_t, _end_t, price_type, _table, id, persona FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM relation_type;
DROP TABLE relation_type;
CREATE TABLE relation_type (
    id int NOT NULL DEFAULT nextval('relation_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO relation_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM credit_card;
DROP TABLE credit_card;
CREATE TABLE credit_card (
    id int NOT NULL DEFAULT nextval('credit_card_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO credit_card (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM chequebook;
DROP TABLE chequebook;
CREATE TABLE chequebook (
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    account bigint,
    id int NOT NULL DEFAULT nextval('chequebook_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX chequebook_account_idx ON chequebook(account);
INSERT INTO chequebook (account, id) SELECT account, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM erm_type;
DROP TABLE erm_type;
CREATE TABLE erm_type (
    id int NOT NULL DEFAULT nextval('erm_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO erm_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_descriptive_item;
DROP TABLE own_descriptive_item;
CREATE TABLE own_descriptive_item (
    amount numeric(12,4),
    description text,
    entry bigint,
    id bigint NOT NULL DEFAULT nextval('own_descriptive_item_id_seq'),
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    own_item bigint,
    account_plan bigint,
    PRIMARY KEY (id)
);
CREATE INDEX own_descriptive_item_own_item_idx ON own_descriptive_item(own_item);
INSERT INTO own_descriptive_item (amount, description, entry, id, own_item, account_plan) SELECT amount, description, entry, id, own_item, account_plan FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _category_type_data;
DROP TABLE _category_type_data;
CREATE TABLE _category_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_category_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _category_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entry;
DROP TABLE entry;
CREATE TABLE entry (
    date timestamp,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    cost_center bigint,
    id bigint NOT NULL DEFAULT nextval('entry_id_seq'),
    entry bigint,
    PRIMARY KEY (id)
);
CREATE INDEX entry_cost_center_idx ON entry(cost_center);
INSERT INTO entry (date, cost_center, id, entry) SELECT date, cost_center, id, entry FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _barter_type_data;
DROP TABLE _barter_type_data;
CREATE TABLE _barter_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    description text,
    id bigint NOT NULL DEFAULT nextval('_barter_type_data_id_seq'),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    account_plan int,
    _end_t timestamp,
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _barter_type_data (_start_t, description, id, _table, account_plan, _end_t, name) SELECT _start_t, description, id, _table, account_plan, _end_t, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM own_document_proceeding;
DROP TABLE own_document_proceeding;
CREATE TABLE own_document_proceeding (
    amount numeric(12,4),
    proceeding bigint,
    entry bigint,
    own_document bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    id bigint NOT NULL DEFAULT nextval('own_document_proceeding_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX own_document_proceeding_proceeding_idx ON own_document_proceeding(proceeding);
CREATE INDEX own_document_proceeding_own_document_idx ON own_document_proceeding(own_document);
INSERT INTO own_document_proceeding (amount, proceeding, entry, own_document, id) SELECT amount, proceeding, entry, own_document, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_currency_type;
DROP TABLE entity_currency_type;
CREATE TABLE entity_currency_type (
    id int NOT NULL DEFAULT nextval('entity_currency_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_currency_type (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _product_provider_data;
DROP TABLE _product_provider_data;
CREATE TABLE _product_provider_data (
    product int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    catalog_number char(30),
    provider int,
    _table bigint NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_product_provider_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX product_provider_provider_idx ON _product_provider_data(provider);
CREATE INDEX product_provider_product_idx ON _product_provider_data(product);
INSERT INTO _product_provider_data (product, _start_t, catalog_number, provider, _table, id, _end_t) SELECT product, _start_t, catalog_number, provider, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM task;
DROP TABLE task;
CREATE TABLE task (
    assigned_time timestamp,
    task_type bigint,
    name varchar(80),
    parent int,
    assigned_by bigint,
    timestamp timestamp DEFAULT current_timestamp NOT NULL,
    task_priority bigint,
    planned_start_time timestamp,
    assigned_to bigint,
    task_status bigint,
    planned_end_time timestamp,
    id int NOT NULL DEFAULT nextval('task_id_seq'),
    PRIMARY KEY (id)
);
CREATE INDEX task_task_status_idx ON task(task_status);
CREATE INDEX task_task_type_idx ON task(task_type);
CREATE INDEX task_parent_idx ON task(parent);
CREATE INDEX task_assigned_by_idx ON task(assigned_by);
CREATE INDEX task_task_priority_idx ON task(task_priority);
CREATE INDEX task_assigned_to_idx ON task(assigned_to);
INSERT INTO task (assigned_time, task_type, name, parent, assigned_by, task_priority, planned_start_time, assigned_to, task_status, planned_end_time, id) SELECT assigned_time, task_type, name, parent, assigned_by, task_priority, planned_start_time, assigned_to, task_status, planned_end_time, id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _category_attribute_data;
DROP TABLE _category_attribute_data;
CREATE TABLE _category_attribute_data (
    category_type int,
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    name varchar(80),
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_category_attribute_data_id_seq'),
    _end_t timestamp,
    PRIMARY KEY (id)
);
CREATE INDEX category_attribute_category_type_idx ON _category_attribute_data(category_type);
INSERT INTO _category_attribute_data (category_type, _start_t, name, _table, id, _end_t) SELECT category_type, _start_t, name, _table, id, _end_t FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_file;
DROP TABLE product_file;
CREATE TABLE product_file (
    id bigint NOT NULL DEFAULT nextval('product_file_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_file (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_measure;
DROP TABLE product_measure;
CREATE TABLE product_measure (
    id int NOT NULL DEFAULT nextval('product_measure_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_measure (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM contact;
DROP TABLE contact;
CREATE TABLE contact (
    id int NOT NULL DEFAULT nextval('contact_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO contact (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM product_tax;
DROP TABLE product_tax;
CREATE TABLE product_tax (
    id bigint NOT NULL DEFAULT nextval('product_tax_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO product_tax (id) SELECT id FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM __papo_patches__;
DROP TABLE __papo_patches__;
CREATE TABLE __papo_patches__ (
    date timestamp DEFAULT now(),
    patch varchar(255),
    PRIMARY KEY (patch)
);
INSERT INTO __papo_patches__ (date, patch) SELECT date, patch FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM _movement_type_data;
DROP TABLE _movement_type_data;
CREATE TABLE _movement_type_data (
    _start_t timestamp DEFAULT '1970/1/1 00:00:00 UTC' NOT NULL,
    _end_t timestamp,
    _table int NOT NULL,
    _state int DEFAULT -1 NOT NULL,
    id bigint NOT NULL DEFAULT nextval('_movement_type_data_id_seq'),
    name varchar(80),
    PRIMARY KEY (id)
);
INSERT INTO _movement_type_data (_start_t, _end_t, _table, id, name) SELECT _start_t, _end_t, _table, id, name FROM temp;
DROP TABLE temp;
CREATE TABLE temp AS SELECT * FROM entity_uid_type;
DROP TABLE entity_uid_type;
CREATE TABLE entity_uid_type (
    id int NOT NULL DEFAULT nextval('entity_uid_type_id_seq'),
    PRIMARY KEY (id)
);
INSERT INTO entity_uid_type (id) SELECT id FROM temp;
DROP TABLE temp;


ALTER TABLE _product_measure_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_measure_data
    ADD FOREIGN KEY (product_measure_type) REFERENCES product_measure_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_measure_data
    ADD FOREIGN KEY (_table) REFERENCES product_measure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_measure_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_file_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_file_data
    ADD FOREIGN KEY (file) REFERENCES file
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_file_data
    ADD FOREIGN KEY (_table) REFERENCES product_file
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_file_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _alien_entity_data
    ADD FOREIGN KEY (_table) REFERENCES alien_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _alien_entity_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _alien_entity_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_measure_type_data
    ADD FOREIGN KEY (_table) REFERENCES product_measure_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_measure_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_task
    ADD FOREIGN KEY (erm) REFERENCES erm
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_task
    ADD FOREIGN KEY (task) REFERENCES task
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE credit_card_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE credit_card_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE credit_card_movement
    ADD FOREIGN KEY (plan) REFERENCES _credit_card_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_entity_type_data
    ADD FOREIGN KEY (category_type) REFERENCES category_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_entity_type_data
    ADD FOREIGN KEY (entity_type) REFERENCES entity_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_entity_type_data
    ADD FOREIGN KEY (_table) REFERENCES category_entity_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_entity_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document_proceeding
    ADD FOREIGN KEY (proceeding) REFERENCES proceeding
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document_proceeding
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document_proceeding
    ADD FOREIGN KEY (alien_document) REFERENCES alien_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_account_data
    ADD FOREIGN KEY (_table) REFERENCES bank_account
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_account_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_account_data
    ADD FOREIGN KEY (bank_branch) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _account_plan_data
    ADD FOREIGN KEY (parent) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _account_plan_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _account_plan_data
    ADD FOREIGN KEY (_table) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_client_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_client_data
    ADD FOREIGN KEY (client) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_client_data
    ADD FOREIGN KEY (employee) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_client_data
    ADD FOREIGN KEY (_table) REFERENCES employee_comm_client
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_client_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (country) REFERENCES country
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (product_classification) REFERENCES product_classification
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (location) REFERENCES physical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_data
    ADD FOREIGN KEY (_table) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE document4document
    ADD FOREIGN KEY (own_document) REFERENCES own_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE document4document
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE document4document
    ADD FOREIGN KEY (alien_document) REFERENCES alien_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_data
    ADD FOREIGN KEY (employee) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_data
    ADD FOREIGN KEY (_table) REFERENCES employee_comm_prod
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_relation_data
    ADD FOREIGN KEY (entity_from) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_relation_data
    ADD FOREIGN KEY (_table) REFERENCES entity_relation
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_relation_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_relation_data
    ADD FOREIGN KEY (entity_to) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_item_tax
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_item_tax
    ADD FOREIGN KEY (tax) REFERENCES _tax_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_item_tax
    ADD FOREIGN KEY (own_item) REFERENCES own_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque_movement
    ADD FOREIGN KEY (cheque) REFERENCES cheque
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _document_type_data
    ADD FOREIGN KEY (_table) REFERENCES document_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _document_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _document_type_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_name_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_name_data
    ADD FOREIGN KEY (product_name_type) REFERENCES product_name_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_name_data
    ADD FOREIGN KEY (_table) REFERENCES product_name
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_name_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_formula_data
    ADD FOREIGN KEY (_table) REFERENCES tax_formula
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_formula_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_formula_data
    ADD FOREIGN KEY (from_profile) REFERENCES tax_profile
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_formula_data
    ADD FOREIGN KEY (to_profile) REFERENCES tax_profile
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE entity_type_uid_conf
    ADD FOREIGN KEY (entity_uid_type) REFERENCES _entity_uid_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE entity_type_uid_conf
    ADD FOREIGN KEY (entity_type) REFERENCES _entity_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_product_item
    ADD FOREIGN KEY (product) REFERENCES _product_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_product_item
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_product_item
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_product_item
    ADD FOREIGN KEY (alien_item) REFERENCES alien_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_data
    ADD FOREIGN KEY (_table) REFERENCES tax
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_data
    ADD FOREIGN KEY (_table) REFERENCES credit_card
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_data
    ADD FOREIGN KEY (accreditation_account) REFERENCES bank_account
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task_recipient
    ADD FOREIGN KEY (task) REFERENCES task
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task_recipient
    ADD FOREIGN KEY (to_make_to) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_attribute_data
    ADD FOREIGN KEY (category_attribute) REFERENCES category_attribute
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_attribute_data
    ADD FOREIGN KEY (_table) REFERENCES entity_attribute
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_attribute_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_attribute_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_descriptive_item
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_descriptive_item
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_descriptive_item
    ADD FOREIGN KEY (alien_item) REFERENCES alien_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque_tax
    ADD FOREIGN KEY (third_party_cheque) REFERENCES third_party_cheque
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque_tax
    ADD FOREIGN KEY (tax) REFERENCES _tax_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_doc4own_doc
    ADD FOREIGN KEY (document_to) REFERENCES own_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_doc4own_doc
    ADD FOREIGN KEY (relation_type) REFERENCES document_relation_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_doc4own_doc
    ADD FOREIGN KEY (document_from) REFERENCES own_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_type_data
    ADD FOREIGN KEY (parent) REFERENCES entity_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_type_data
    ADD FOREIGN KEY (_table) REFERENCES entity_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document_tax
    ADD FOREIGN KEY (own_document) REFERENCES own_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document_tax
    ADD FOREIGN KEY (tax) REFERENCES _tax_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _relation_types_data
    ADD FOREIGN KEY (entity_relation) REFERENCES entity_relation
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _relation_types_data
    ADD FOREIGN KEY (_table) REFERENCES relation_types
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _relation_types_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _relation_types_data
    ADD FOREIGN KEY (relation_type) REFERENCES relation_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_profile_data
    ADD FOREIGN KEY (tax) REFERENCES tax
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_profile_data
    ADD FOREIGN KEY (_table) REFERENCES tax_profile
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _tax_profile_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_data
    ADD FOREIGN KEY (_table) REFERENCES own_pos
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_data
    ADD FOREIGN KEY (own_entity) REFERENCES own_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_branch_data
    ADD FOREIGN KEY (own_entity) REFERENCES own_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_branch_data
    ADD FOREIGN KEY (_table) REFERENCES own_branch
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_branch_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_data
    ADD FOREIGN KEY (persona) REFERENCES persona
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_data
    ADD FOREIGN KEY (_table) REFERENCES bank
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _obligation_type_data
    ADD FOREIGN KEY (_table) REFERENCES obligation_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _obligation_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _obligation_type_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE proceeding
    ADD FOREIGN KEY (responsable) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE proceeding
    ADD FOREIGN KEY (branch) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE proceeding
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_code_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_code_data
    ADD FOREIGN KEY (product_code_type) REFERENCES product_code_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_code_data
    ADD FOREIGN KEY (_table) REFERENCES product_code
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_code_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical2physical_data
    ADD FOREIGN KEY (_table) REFERENCES logical2physical
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical2physical_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical2physical_data
    ADD FOREIGN KEY (logical) REFERENCES logical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical2physical_data
    ADD FOREIGN KEY (physical) REFERENCES physical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_data
    ADD FOREIGN KEY (currency_type) REFERENCES currency_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_data
    ADD FOREIGN KEY (price_type) REFERENCES price_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_data
    ADD FOREIGN KEY (_table) REFERENCES price
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_data
    ADD FOREIGN KEY (own_entity) REFERENCES own_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_data
    ADD FOREIGN KEY (_table) REFERENCES employee
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document_tax
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document_tax
    ADD FOREIGN KEY (tax) REFERENCES _tax_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document_tax
    ADD FOREIGN KEY (alien_document) REFERENCES alien_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_item
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_item
    ADD FOREIGN KEY (own_document) REFERENCES own_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_item
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _role_data
    ADD FOREIGN KEY (_table) REFERENCES role
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _role_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE movement
    ADD FOREIGN KEY (proceeding) REFERENCES proceeding
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE movement
    ADD FOREIGN KEY (parent) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE movement
    ADD FOREIGN KEY (movement_type) REFERENCES _movement_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque
    ADD FOREIGN KEY (bank_branch) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _alien_storehouse_data
    ADD FOREIGN KEY (_table) REFERENCES alien_storehouse
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _alien_storehouse_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _alien_storehouse_data
    ADD FOREIGN KEY (alien_entity) REFERENCES alien_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_tax_profile_data
    ADD FOREIGN KEY (tax_profile) REFERENCES tax_profile
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_tax_profile_data
    ADD FOREIGN KEY (persona) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_tax_profile_data
    ADD FOREIGN KEY (_table) REFERENCES persona_tax_profile
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_tax_profile_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (own_document_type) REFERENCES _document_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (salesperson) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (recipient) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (transport) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (own_entity) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (authorized_by) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_client_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_client_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_client_data
    ADD FOREIGN KEY (client) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_client_data
    ADD FOREIGN KEY (employee) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_client_data
    ADD FOREIGN KEY (_table) REFERENCES employee_comm_prod_client
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_comm_prod_client_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_product_stock_item
    ADD FOREIGN KEY (own_item) REFERENCES own_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_product_stock_item
    ADD FOREIGN KEY (product) REFERENCES _product_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_log
    ADD FOREIGN KEY (erm) REFERENCES erm
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _task_priority_data
    ADD FOREIGN KEY (_table) REFERENCES task_priority
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _task_priority_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE obligation
    ADD FOREIGN KEY (obligation_type) REFERENCES _obligation_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE obligation
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (salesperson) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (received_by) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (own_entity) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (transport) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (alien_document_type) REFERENCES _document_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (sender) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_document
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_tax_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_tax_data
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_tax_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_tax_data
    ADD FOREIGN KEY (_table) REFERENCES product_tax
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_tax_data
    ADD FOREIGN KEY (tax) REFERENCES tax
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_branch_data
    ADD FOREIGN KEY (persona) REFERENCES persona
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_branch_data
    ADD FOREIGN KEY (_table) REFERENCES bank_branch
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _bank_branch_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _address_data
    ADD FOREIGN KEY (_table) REFERENCES address
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _address_data
    ADD FOREIGN KEY (address_type) REFERENCES address_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _address_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _address_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _relation_type_data
    ADD FOREIGN KEY (_table) REFERENCES relation_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _relation_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _physical_structure_data
    ADD FOREIGN KEY (parent) REFERENCES physical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _physical_structure_data
    ADD FOREIGN KEY (_table) REFERENCES physical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _physical_structure_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical_structure_data
    ADD FOREIGN KEY (parent) REFERENCES logical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical_structure_data
    ADD FOREIGN KEY (_table) REFERENCES logical_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _logical_structure_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _currency_type_data
    ADD FOREIGN KEY (_table) REFERENCES currency_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _currency_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _currency_type_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _country_data
    ADD FOREIGN KEY (_table) REFERENCES country
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _country_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque
    ADD FOREIGN KEY (chequebook) REFERENCES chequebook
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE bank_account_movement
    ADD FOREIGN KEY (origin) REFERENCES _bank_account_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE bank_account_movement
    ADD FOREIGN KEY (destination) REFERENCES _bank_account_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE bank_account_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _task_status_data
    ADD FOREIGN KEY (_table) REFERENCES task_status
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _task_status_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _file_data
    ADD FOREIGN KEY (_table) REFERENCES file
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _file_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_item
    ADD FOREIGN KEY (alien_document) REFERENCES alien_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_item
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_item
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_item_tax
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_item_tax
    ADD FOREIGN KEY (tax) REFERENCES _tax_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_item_tax
    ADD FOREIGN KEY (alien_item) REFERENCES alien_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_limit_data
    ADD FOREIGN KEY (movement_type) REFERENCES movement_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_limit_data
    ADD FOREIGN KEY (_table) REFERENCES credit_limit
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_limit_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_limit_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_data
    ADD FOREIGN KEY (ticket_type) REFERENCES ticket_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_data
    ADD FOREIGN KEY (_table) REFERENCES ticket
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_data
    ADD FOREIGN KEY (_table) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE currency_movement
    ADD FOREIGN KEY (currency_type) REFERENCES _currency_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE currency_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE currency_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_entity_data
    ADD FOREIGN KEY (_table) REFERENCES own_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_entity_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_entity_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque_tax
    ADD FOREIGN KEY (tax) REFERENCES _tax_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE cheque_tax
    ADD FOREIGN KEY (cheque) REFERENCES cheque
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_storehouse_data
    ADD FOREIGN KEY (own_entity) REFERENCES own_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_storehouse_data
    ADD FOREIGN KEY (_table) REFERENCES own_storehouse
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_storehouse_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_store_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_store_data
    ADD FOREIGN KEY (storehouse) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_store_data
    ADD FOREIGN KEY (_table) REFERENCES product_store
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_store_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE barter_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE barter_movement
    ADD FOREIGN KEY (barter) REFERENCES _barter_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE barter_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_type_data
    ADD FOREIGN KEY (_table) REFERENCES ticket_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _ticket_type_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _provider_data
    ADD FOREIGN KEY (persona) REFERENCES persona
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _provider_data
    ADD FOREIGN KEY (_table) REFERENCES provider
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _provider_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_product_item
    ADD FOREIGN KEY (own_item) REFERENCES own_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_product_item
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_product_item
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_product_item
    ADD FOREIGN KEY (price) REFERENCES _price_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_data
    ADD FOREIGN KEY (_table) REFERENCES barter
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_data
    ADD FOREIGN KEY (barter_type) REFERENCES barter_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_type_data
    ADD FOREIGN KEY (parent) REFERENCES price_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_type_data
    ADD FOREIGN KEY (branch) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _price_type_data
    ADD FOREIGN KEY (_table) REFERENCES price_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE obligation_movement
    ADD FOREIGN KEY (obligation) REFERENCES obligation
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE obligation_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE obligation_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _organization_data
    ADD FOREIGN KEY (own_entity) REFERENCES own_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _organization_data
    ADD FOREIGN KEY (_table) REFERENCES organization
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _organization_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm
    ADD FOREIGN KEY (erm_type) REFERENCES _erm_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm
    ADD FOREIGN KEY (made_by) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_uid_type_data
    ADD FOREIGN KEY (_table) REFERENCES entity_uid_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_uid_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _comm_prod_client_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _comm_prod_client_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _comm_prod_client_data
    ADD FOREIGN KEY (client) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _comm_prod_client_data
    ADD FOREIGN KEY (_table) REFERENCES comm_prod_client
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _comm_prod_client_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE ticket_movement
    ADD FOREIGN KEY (ticket) REFERENCES _ticket_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE ticket_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE ticket_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_code_type_data
    ADD FOREIGN KEY (_table) REFERENCES product_code_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_code_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_file
    ADD FOREIGN KEY (erm) REFERENCES erm
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_file
    ADD FOREIGN KEY (file) REFERENCES _file_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _task_type_data
    ADD FOREIGN KEY (_table) REFERENCES task_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _task_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque_movement
    ADD FOREIGN KEY (third_party_cheque) REFERENCES third_party_cheque
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque_movement
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE third_party_cheque_movement
    ADD FOREIGN KEY (movement) REFERENCES movement
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_entity
    ADD FOREIGN KEY (erm) REFERENCES erm
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE erm_entity
    ADD FOREIGN KEY (made_to) REFERENCES _entity_types_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _contact_data
    ADD FOREIGN KEY (_table) REFERENCES contact
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _contact_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _contact_data
    ADD FOREIGN KEY (alien_entity) REFERENCES alien_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_types_data
    ADD FOREIGN KEY (entity_type) REFERENCES entity_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_types_data
    ADD FOREIGN KEY (_table) REFERENCES entity_types
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_types_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_types_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_document_data
    ADD FOREIGN KEY (document_type) REFERENCES document_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_document_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_document_data
    ADD FOREIGN KEY (_table) REFERENCES own_pos_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _own_pos_document_data
    ADD FOREIGN KEY (own_pos) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_name_type_data
    ADD FOREIGN KEY (_table) REFERENCES product_name_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_name_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_plan_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_plan_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_plan_data
    ADD FOREIGN KEY (credit_card) REFERENCES credit_card
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _credit_card_plan_data
    ADD FOREIGN KEY (_table) REFERENCES credit_card_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_structure_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_structure_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_structure_data
    ADD FOREIGN KEY (_table) REFERENCES product_structure
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_structure_data
    ADD FOREIGN KEY (component) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_doc4alien_doc
    ADD FOREIGN KEY (document_to) REFERENCES alien_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_doc4alien_doc
    ADD FOREIGN KEY (relation_type) REFERENCES document_relation_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE alien_doc4alien_doc
    ADD FOREIGN KEY (document_from) REFERENCES alien_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_role_data
    ADD FOREIGN KEY (employee) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_role_data
    ADD FOREIGN KEY (role) REFERENCES role
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_role_data
    ADD FOREIGN KEY (_table) REFERENCES employee_role
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _employee_role_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _erm_type_data
    ADD FOREIGN KEY (_table) REFERENCES erm_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _erm_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _sys_account2account_plan_data
    ADD FOREIGN KEY (account) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _sys_account2account_plan_data
    ADD FOREIGN KEY (_table) REFERENCES sys_account2account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _sys_account2account_plan_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _address_type_data
    ADD FOREIGN KEY (_table) REFERENCES address_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _address_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_data
    ADD FOREIGN KEY (_table) REFERENCES persona
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _persona_data
    ADD FOREIGN KEY (alien_entity) REFERENCES alien_entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_currency_type_data
    ADD FOREIGN KEY (currency_type) REFERENCES currency_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_currency_type_data
    ADD FOREIGN KEY (_table) REFERENCES entity_currency_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_currency_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_currency_type_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_classification_data
    ADD FOREIGN KEY (parent) REFERENCES product_classification
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_classification_data
    ADD FOREIGN KEY (_table) REFERENCES product_classification
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_classification_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_uid_data
    ADD FOREIGN KEY (_table) REFERENCES entity_uid
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_uid_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_uid_data
    ADD FOREIGN KEY (entity_uid_type) REFERENCES entity_uid_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _entity_uid_data
    ADD FOREIGN KEY (entity) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _commission_data
    ADD FOREIGN KEY (_table) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _commission_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _client_data
    ADD FOREIGN KEY (commission) REFERENCES commission
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _client_data
    ADD FOREIGN KEY (price_type) REFERENCES price_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _client_data
    ADD FOREIGN KEY (_table) REFERENCES client
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _client_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _client_data
    ADD FOREIGN KEY (persona) REFERENCES persona
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE chequebook
    ADD FOREIGN KEY (account) REFERENCES _bank_account_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_descriptive_item
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_descriptive_item
    ADD FOREIGN KEY (own_item) REFERENCES own_item
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_descriptive_item
    ADD FOREIGN KEY (account_plan) REFERENCES _account_plan_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_type_data
    ADD FOREIGN KEY (_table) REFERENCES category_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE entry
    ADD FOREIGN KEY (cost_center) REFERENCES _logical_structure_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE entry
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_type_data
    ADD FOREIGN KEY (_table) REFERENCES barter_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _barter_type_data
    ADD FOREIGN KEY (account_plan) REFERENCES account_plan
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document_proceeding
    ADD FOREIGN KEY (proceeding) REFERENCES proceeding
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document_proceeding
    ADD FOREIGN KEY (entry) REFERENCES entry
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE own_document_proceeding
    ADD FOREIGN KEY (own_document) REFERENCES own_document
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_provider_data
    ADD FOREIGN KEY (product) REFERENCES product
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_provider_data
    ADD FOREIGN KEY (provider) REFERENCES entity
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_provider_data
    ADD FOREIGN KEY (_table) REFERENCES product_provider
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _product_provider_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task
    ADD FOREIGN KEY (task_type) REFERENCES _task_type_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task
    ADD FOREIGN KEY (parent) REFERENCES task
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task
    ADD FOREIGN KEY (assigned_by) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task
    ADD FOREIGN KEY (task_priority) REFERENCES _task_priority_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task
    ADD FOREIGN KEY (assigned_to) REFERENCES _entity_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE task
    ADD FOREIGN KEY (task_status) REFERENCES _task_status_data
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_attribute_data
    ADD FOREIGN KEY (category_type) REFERENCES category_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_attribute_data
    ADD FOREIGN KEY (_table) REFERENCES category_attribute
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _category_attribute_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _movement_type_data
    ADD FOREIGN KEY (_table) REFERENCES movement_type
    ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE _movement_type_data
    ADD FOREIGN KEY (_state) REFERENCES __state__
    ON DELETE RESTRICT ON UPDATE RESTRICT;


COMMIT;

