alter table if exists search_tasks
  alter column account_id drop not null;

do $$
declare
  constraint_name text;
begin
  select conname
    into constraint_name
  from pg_constraint
  where conrelid = 'search_tasks'::regclass
    and contype = 'f'
    and pg_get_constraintdef(oid) like '%procurement_accounts_1688%';

  if constraint_name is not null then
    execute format('alter table search_tasks drop constraint %I', constraint_name);
  end if;
end $$;

alter table if exists search_tasks
  add constraint search_tasks_account_id_fkey
  foreign key (account_id)
  references procurement_accounts_1688(id)
  on delete set null;
