       CASE WHEN array_length(t.path,1) = 1
         THEN e.max
         ELSE t.max
-      END AS max
-    FROM fhir.expanded_resource_elements e
+      END AS max,
+      CASE WHEN array_length(t.path,1) = 1
+        THEN e.type
+        ELSE t.type
+      END AS type
+    FROM fhir.polimorphic_expanded_resource_elements e
     JOIN fhir.datatype_unified_elements t
     ON t.path[1] = e.type
 );


-- expand polimorphic types
CREATE
VIEW fhir.expanded_resource_elements as (
  SELECT
    array_pop(path) || ARRAY[column_name(array_last(path), type)] as path,
    type,


-- get all elements wich have a children
-- all parent path is coumpound (teorema Bodnarchuka)
/* CREATE */
/* VIEW fhir.compound_resource_elements as ( */
/*   SELECT a.* */
/*          ,ere.min */
/*          ,ere.max */
/*     FROM ( */
/*             SELECT DISTINCT */
/*               array_pop(path) as path */
/*             FROM fhir.polimorphic_expanded_resource_elements */
/*             WHERE array_length(path,1) > 1 */
/*          ) a */
/*     LEFT JOIN fhir.polimorphic_expanded_resource_elements ere */
/*     ON ere.path = a.path */
/* ); */

-- elements recursively expanded with complex datatypes
CREATE
VIEW fhir.expanded_resource_elements as (
    SELECT
      e.path || array_tail(t.path) as path,
      CASE WHEN array_length(t.path,1) = 1
