There was an SQL error: ERROR: column "location_id" of relation "cache_occurrences" does not exist at character 648 - insert into cache_occurrences ( id, record_status, downloaded_flag, zero_abundance, website_id, survey_id, sample_id, survey_title, date_start, date_end, date_type, public_entered_sref, entered_sref_system, public_geom, sample_method, taxa_taxon_list_id, preferred_taxa_taxon_list_id, taxonomic_sort_order, taxon, authority, preferred_taxon, preferred_authority, default_common_name, search_name, taxa_taxon_list_external_key, taxon_meaning_id, taxon_group_id, taxon_group, created_by_id, cache_created_on, cache_updated_on, certainty, location_name, recorders, verifier, images, training, location_id ) select distinct on (o.id) o.id, o.record_status, o.downloaded_flag, o.zero_abundance, su.website_id as website_id, su.id as survey_id, s.id as sample_id, su.title as survey_title, s.date_start, s.date_end, s.date_type, case when o.confidential=true or o.sensitivity_precision is not null then null else coalesce(s.entered_sref, l.centroid_sref) end as public_entered_sref, case when s.entered_sref_system is null then l.centroid_sref_system else s.entered_sref_system end as entered_sref_system, reduce_precision(coalesce(s.geom, l.centroid_geom), o.confidential, o.sensitivity_precision, case when s.entered_sref_system is null then l.centroid_sref_system else s.entered_sref_system end) as public_geom, tmethod.term as sample_method, cttl.id as taxa_taxon_list_id, cttl.preferred_taxa_taxon_list_id, cttl.taxonomic_sort_order, cttl.taxon, cttl.authority, cttl.preferred_taxon, cttl.preferred_authority, cttl.default_common_name, cttl.search_name, cttl.external_key as taxa_taxon_list_external_key, cttl.taxon_meaning_id, cttl.taxon_group_id, cttl.taxon_group, o.created_by_id, now(), now(), case when certainty.sort_order is null then null when certainty.sort_order <100 then 'C' when certainty.sort_order <200 then 'L' else 'U' end, case when o.confidential=true or o.sensitivity_precision is not null then null else coalesce(l.name, s.location_name) end, s.recorder_names, pv.surname || ', ' || pv.first_name, images.list, o.training, s.location_id from occurrences o left join cache_occurrences co on co.id=o.id join samples s on s.id=o.sample_id left join locations l on l.id=s.location_id and l.deleted=false join surveys su on su.id=s.survey_id left join cache_termlists_terms tmethod on tmethod.id=s.sample_method_id join cache_taxa_taxon_lists cttl on cttl.id=o.taxa_taxon_list_id left join (occurrence_attribute_values oav join termlists_terms certainty on certainty.id=oav.int_value join occurrence_attributes oa on oa.id=oav.occurrence_attribute_id and oa.deleted='f' and oa.system_function='certainty' ) on oav.occurrence_id=o.id and oav.deleted='f' left join users uv on uv.id=o.verified_by_id and uv.deleted=false left join people pv on pv.id=uv.person_id and pv.deleted=false left join (select occurrence_id, array_to_string(array_agg(path), ',') as list from occurrence_images where deleted=false group by occurrence_id) as images on images.occurrence_id=o.id where co.id is null and o.id=77