topfans/backend/scripts/migrations/migrate_fix_assets_id_seq.sql

15 lines
645 B
SQL
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.

-- =============================================================================
-- 修复 assets 主键序列与 MAX(id) 不同步 → duplicate key assets_pkey (23505)
--
-- 用法:在 Navicat / DBeaver / pgAdmin 中只执行下面这一条 SELECT不要带 BEGIN/COMMIT
-- 验证:
-- SELECT MAX(id) FROM public.assets;
-- SELECT last_value FROM public.assets_id_seq;
-- last_value 应 >= MAX(id)
-- =============================================================================
SELECT setval(
pg_get_serial_sequence('public.assets', 'id'),
COALESCE((SELECT MAX(id) FROM public.assets), 0)
) AS assets_id_seq_synced_to;