Files
bl/help/修改自增id.sql
xinian 9cda69c23f
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
编辑文件 修改自增id.sql
2026-03-01 00:53:55 +08:00

27 lines
661 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

select setval('base_sys_user_id_seq', 10000000, false);
-- 1. 恢复原自增解除依赖
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT nextval('base_sys_user_id_seq');
-- 2. 清理旧函数
DROP FUNCTION IF EXISTS next_8digit_rand_id() CASCADE;
-- 3. 终极版不重复 + 乱序 + 固定8位
CREATE OR REPLACE FUNCTION next_8digit_rand_id()
RETURNS bigint
LANGUAGE plpgsql VOLATILE
AS $$
DECLARE
seq_val bigint;
BEGIN
seq_val := nextval('base_sys_user_id_seq');
RETURN 10000000 + ((seq_val * 1103515245 + 12345) # 58329) % 90000000;
END;
$$;
-- 4. 启用
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT next_8digit_rand_id();