From 4dc09c559a2bada4480a320068ad43a208989239 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Fri, 17 Feb 2023 17:29:12 +0100 Subject: [PATCH] fix setval with coalesce (reset id too if no rows) --- sql/import.sql | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sql/import.sql b/sql/import.sql index b33c8f5..91428e5 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -155,32 +155,32 @@ SELECT setval('users_id_seq', (SELECT COALESCE(max(id),1) FROM users)); -- Undo 11. DELETE FROM scopes USING import.choix_periodes icp WHERE scopes.name::jsonb = icp.acp_scopes1::jsonb; -SELECT setval('scopes_id_seq', (SELECT max(id) FROM scopes)); +SELECT setval('scopes_id_seq', (SELECT COALESCE(max(id),1) FROM scopes)); ALTER TABLE import.choix_periodes DROP COLUMN acp_scopes1; -- Undo 10. DELETE FROM chill_main_user_job cmuj USING import.choix_periodes icp WHERE cmuj.label::jsonb = icp.job1::jsonb; -SELECT setval('chill_main_user_job_id_seq', (SELECT max(id) FROM chill_main_user_job)); +SELECT setval('chill_main_user_job_id_seq', (SELECT COALESCE(max(id),1) FROM chill_main_user_job)); ALTER TABLE import.choix_periodes DROP COLUMN job1; -- Undo 9. DELETE FROM chill_person_accompanying_period_origin cpapo USING import.choix_periodes icp WHERE cpapo.label::jsonb = icp.origin1::jsonb; -SELECT setval('chill_person_accompanying_period_origin_id_seq', (SELECT max(id) FROM chill_person_accompanying_period_origin)); +SELECT setval('chill_person_accompanying_period_origin_id_seq', (SELECT COALESCE(max(id),1) FROM chill_person_accompanying_period_origin)); ALTER TABLE import.choix_periodes DROP COLUMN origin1; -- Undo 8. DELETE FROM chill_person_accompanying_period_closingmotive cpapcm USING import.choix_periodes icp WHERE cpapcm.name::jsonb = icp.closingmotive1::jsonb; -SELECT setval('chill_person_accompanying_period_closingmotive_id_seq', (SELECT max(id) FROM chill_person_accompanying_period_closingmotive)); +SELECT setval('chill_person_accompanying_period_closingmotive_id_seq', (SELECT COALESCE(max(id),1) FROM chill_person_accompanying_period_closingmotive)); ALTER TABLE import.choix_periodes DROP COLUMN closingmotive1; -- Undo 7. DELETE FROM chill_person_household_position cphp USING import.choix_personnes icp WHERE cphp.label::jsonb = icp.household_position1::jsonb; -SELECT setval('chill_person_household_position_id_seq', (SELECT max(id) FROM chill_person_household_position)); +SELECT setval('chill_person_household_position_id_seq', (SELECT COALESCE(max(id),1) FROM chill_person_household_position)); ALTER TABLE import.choix_personnes DROP COLUMN household_position1; -- Undo 6. DELETE FROM chill_person_household_composition_type cphct USING import.choix_personnes icp WHERE cphct.label::jsonb = icp.household_composition_type1::jsonb AND cphct.id > 6; -SELECT setval('chill_person_household_composition_type_id_seq', (SELECT max(id) FROM chill_person_household_composition_type)); +SELECT setval('chill_person_household_composition_type_id_seq', (SELECT COALESCE(max(id),1) FROM chill_person_household_composition_type)); ALTER TABLE import.choix_personnes DROP COLUMN household_composition_type1; -- Undo 5. @@ -189,12 +189,12 @@ ALTER TABLE import.choix_personnes DROP COLUMN maritalstatus1; -- Undo 4. DELETE FROM country USING import.choix_personnes icp WHERE country.name::jsonb = icp.country1::jsonb AND country.id > 249; -- 249 existing countries -SELECT setval('country_id_seq', (SELECT max(id) FROM country)); +SELECT setval('country_id_seq', (SELECT COALESCE(max(id),1) FROM country)); ALTER TABLE import.choix_personnes DROP COLUMN country1; -- Undo 3. DELETE FROM chill_main_civility cmc USING import.choix_personnes ip WHERE cmc.name::jsonb = ip.civility1::jsonb; -SELECT setval('chill_main_civility_id_seq', (SELECT max(id) FROM chill_main_civility)); +SELECT setval('chill_main_civility_id_seq', (SELECT COALESCE(max(id),1) FROM chill_main_civility)); ALTER TABLE import.choix_personnes DROP COLUMN civility1; -- Undo 2.