ALTER TABLE transaction_entry
    DROP user_id,
    DROP station_id;


ALTER TABLE account ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE account_transaction ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE address ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE attachment ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE bank_account ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE bill_option ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE branch ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE branch_station ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE calls ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE card_operation_cost ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE card_payment_device ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE cfop_data ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE check_data ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE client_category ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE client_category_price ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE client ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE client_salary_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE commission ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE commission_source ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE company ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE contact_info ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE cost_center ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE cost_center_entry ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE credit_card_data ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE credit_check_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE credit_provider ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE delivery ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE device_constant ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE device_settings ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE employee ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE employee_role ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE employee_role_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE fiscal_book_entry ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE fiscal_day_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE fiscal_day_tax ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE fiscal_sale_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE image ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE individual ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE installed_plugin ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE inventory ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE inventory_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE invoice_field ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE invoice_layout ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE invoice_printer ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE loan ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE loan_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE login_user ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE military_data ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE parameter_data ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_category ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_change_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_comment ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_flow_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_group ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_method ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE payment_renegotiation ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE person ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_component ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_icms_template ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE production_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE production_item_quality_result ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE production_material ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE production_order ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE production_produced_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE production_service ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_ipi_template ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_manufacturer ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_quality_test ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_stock_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_supplier_info ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE product_tax_template ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE profile_settings ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE purchase_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE purchase_order ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE purchase_receiving_map ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE quotation ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE quote_group ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE receiving_order ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE receiving_order_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE returned_sale ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE returned_sale_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sale_comment ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sale ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sale_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sale_item_icms ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sale_item_ipi ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sales_person ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sellable_category ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sellable ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sellable_tax_constant ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE sellable_unit ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE service ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE stock_decrease ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE stock_decrease_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE stock_transaction_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE storable_batch ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE storable ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE supplier ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE till ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE till_entry ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE transfer_order ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE transfer_order_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE transporter ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE ui_field ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE ui_form ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE user_branch_access ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE user_profile ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE voter_data ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_order_category ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_order ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_order_history ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_order_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_order_package ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_order_package_item ALTER COLUMN te_id SET DEFAULT new_te();
ALTER TABLE work_permit_data ALTER COLUMN te_id SET DEFAULT new_te();

CREATE RULE update_te AS ON UPDATE TO account DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO account_transaction DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO address DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO attachment DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO bank_account DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO bill_option DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO branch DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO branch_station DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO calls DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO card_operation_cost DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO card_payment_device DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO cfop_data DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO check_data DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO client_category DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO client_category_price DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO client DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO client_salary_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO commission DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO commission_source DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO company DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO contact_info DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO cost_center DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO cost_center_entry DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO credit_card_data DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO credit_check_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO credit_provider DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO delivery DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO device_constant DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO device_settings DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO employee DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO employee_role DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO employee_role_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO fiscal_book_entry DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO fiscal_day_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO fiscal_day_tax DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO fiscal_sale_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO image DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO individual DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO installed_plugin DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO inventory DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO inventory_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO invoice_field DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO invoice_layout DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO invoice_printer DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO loan DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO loan_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO login_user DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO military_data DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO parameter_data DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_category DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_change_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_comment DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_flow_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_group DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_method DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO payment_renegotiation DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO person DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_component DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_icms_template DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO production_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO production_item_quality_result DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO production_material DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO production_order DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO production_produced_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO production_service DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_ipi_template DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_manufacturer DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_quality_test DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_stock_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_supplier_info DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO product_tax_template DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO profile_settings DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO purchase_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO purchase_order DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO purchase_receiving_map DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO quotation DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO quote_group DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO receiving_order DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO receiving_order_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO returned_sale DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO returned_sale_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sale_comment DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sale DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sale_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sale_item_icms DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sale_item_ipi DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sales_person DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sellable_category DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sellable DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sellable_tax_constant DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO sellable_unit DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO service DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO stock_decrease DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO stock_decrease_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO stock_transaction_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO storable_batch DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO storable DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO supplier DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO till DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO till_entry DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO transfer_order DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO transfer_order_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO transporter DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO ui_field DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO ui_form DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO user_branch_access DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO user_profile DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO voter_data DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_order_category DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_order DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_order_history DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_order_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_order_package DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_order_package_item DO ALSO SELECT update_te(old.te_id);
CREATE RULE update_te AS ON UPDATE TO work_permit_data DO ALSO SELECT update_te(old.te_id);
