BEGIN;

-- Fix ALL model lines that have NO explicit tax set.
-- These inherit the account's default tax (HST PURCHASE) which is WRONG
-- for bank fees, foreign vendors, insurance, interest, etc.
-- Set them all to NO TAX PURCHASE (ID 32) explicitly.

INSERT INTO account_reconcile_model_line_account_tax_rel (account_reconcile_model_line_id, account_tax_id)
SELECT rml.id, 32
FROM account_reconcile_model rm
JOIN account_reconcile_model_line rml ON rml.model_id = rm.id
LEFT JOIN account_reconcile_model_line_account_tax_rel tr ON tr.account_reconcile_model_line_id = rml.id
WHERE rm.active = true AND rm.company_id = 1
  AND tr.account_tax_id IS NULL
ON CONFLICT DO NOTHING;

COMMIT;
