fix(pgsql): tie PK sequence to nextval default, setval past data, keep serial defaults
- derive the primary key sequence from the column's nextval() default instead of an unused identity_<table>_<pk> sequence - setval after table creation (full and diff paths), forward-only, MAX+1 with is_called=false - do not DROP DEFAULT on serial/bigserial columns without a model default
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
DO $$
|
||||
DECLARE
|
||||
m_cnt bigint;
|
||||
m_next bigint;
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM pg_class c
|
||||
@@ -12,8 +13,15 @@ BEGIN
|
||||
SELECT COALESCE(MAX({{quote_ident .ColumnName}}), 0) + 1
|
||||
FROM {{qual_table .SchemaName .TableName}}
|
||||
INTO m_cnt;
|
||||
|
||||
PERFORM setval('{{qual_table_raw .SchemaName .SequenceName}}'::regclass, m_cnt);
|
||||
|
||||
SELECT CASE WHEN is_called THEN last_value + 1 ELSE last_value END
|
||||
FROM {{qual_table .SchemaName .SequenceName}}
|
||||
INTO m_next;
|
||||
|
||||
-- Only move the sequence forward; never rewind one that is already past the data.
|
||||
IF m_cnt > m_next THEN
|
||||
PERFORM setval('{{qual_table_raw .SchemaName .SequenceName}}'::regclass, m_cnt, false);
|
||||
END IF;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
Reference in New Issue
Block a user