From 0789440e67577a08b22525760f689f38c43eb105 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 20 Jan 2017 18:22:17 +0100 Subject: Fix treatment view (refs #3426) --- archaeological_finds/models_treatments.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'archaeological_finds/models_treatments.py') diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index dacfbfdfc..d8d51e28c 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -310,9 +310,10 @@ class FindUpstreamTreatments(AbsFindTreatments): WITH RECURSIVE rel_tree AS ( SELECT id AS find_id, upstream_treatment_id, downstream_treatment_id, 1 AS level, - array[upstream_treatment_id] AS path_info + ARRAY[]::integer[] AS path_info FROM archaeological_finds_find - WHERE upstream_treatment_id is null + WHERE upstream_treatment_id is NULL AND + downstream_treatment_id is NOT NULL UNION ALL SELECT c.id AS find_id, c.upstream_treatment_id, c.downstream_treatment_id, @@ -320,8 +321,10 @@ class FindUpstreamTreatments(AbsFindTreatments): FROM archaeological_finds_find c JOIN rel_tree p ON c.upstream_treatment_id = p.downstream_treatment_id - AND c.upstream_treatment_id != - ALL(p.path_info[0:array_upper(p.path_info, 1)-1]) + AND (p.path_info = ARRAY[]::integer[] OR + NOT (c.upstream_treatment_id = + ANY(p.path_info[0:array_upper(p.path_info, 1)-1])) + ) ) SELECT DISTINCT find_id, path_info, level FROM rel_tree ORDER BY find_id; @@ -364,9 +367,10 @@ class FindDownstreamTreatments(AbsFindTreatments): WITH RECURSIVE rel_tree AS ( SELECT id AS find_id, downstream_treatment_id, upstream_treatment_id, 1 AS level, - array[downstream_treatment_id] AS path_info + ARRAY[]::integer[] AS path_info FROM archaeological_finds_find - WHERE downstream_treatment_id is null + WHERE downstream_treatment_id is NULL AND + upstream_treatment_id is NOT NULL UNION ALL SELECT c.id AS find_id, c.downstream_treatment_id, c.upstream_treatment_id, @@ -374,8 +378,10 @@ class FindDownstreamTreatments(AbsFindTreatments): FROM archaeological_finds_find c JOIN rel_tree p ON c.downstream_treatment_id = p.upstream_treatment_id - AND c.downstream_treatment_id != - ALL(p.path_info[0:array_upper(p.path_info, 1)-1]) + AND (p.path_info = ARRAY[]::integer[] OR + NOT (c.downstream_treatment_id = + ANY(p.path_info[0:array_upper(p.path_info, 1)-1])) + ) ) SELECT DISTINCT find_id, path_info, level FROM rel_tree ORDER BY find_id; -- cgit v1.2.3