From 0452bde160f338bacbab00ef4be18fd56e7cb89c Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 5 Nov 2020 22:30:28 -0500 Subject: [PATCH] sort the columns normally except push the primary key(s) into the first positiong --- sql/build_master_tables.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/build_master_tables.sql b/sql/build_master_tables.sql index af14deb..1717d2e 100644 --- a/sql/build_master_tables.sql +++ b/sql/build_master_tables.sql @@ -10,11 +10,11 @@ BEGIN SELECT -------------------------------------------create table--------------------------------------------------------------------------------------------------------- 'DROP TABLE IF EXISTS fc.'||func||' CASCADE; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || - string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) || + string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY CASE WHEN fkey IS NOT NULL THEN 0 ELSE opos END ASC) || ', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl, -------------------------------------------populate table------------------------------------------------------------------------------------------------------- ---need to add a clause to exclude where the key is null - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM '||tname||' WHERE '|| + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY CASE WHEN fkey IS NOT NULL THEN 0 ELSE opos END ASC) || ' FROM '||tname||' WHERE '|| string_agg(format('%I',cname)||' IS NOT NULL ',' AND ') FILTER (WHERE fkey = func)||' ON CONFLICT DO NOTHING' AS pop, -------------------------------------------setup foreign keys--------------------------------------------------------------------------------------------------- 'ALTER TABLE fc.live ADD CONSTRAINT fk_'||func||' FOREIGN KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||') REFERENCES fc.'||func||' ('||