Skip to content

1.现象

从测试数据库导出单表,往正式里面导入,出现了插入数据的时候不会id不会自增(默认是自动增长)

1.1 查看数据库seq

1.当前数据库

sql
SELECT sequence_name
FROM information_schema.sequences
WHERE sequence_schema = 'public'; -- 如果你想查看特定模式下的序列,可以修改这里的模式名,查看是所有
SELECT sequence_name
FROM information_schema.sequences
WHERE sequence_schema = 'public'; -- 如果你想查看特定模式下的序列,可以修改这里的模式名,查看是所有

2.根据表名

sql
SELECT c.relname AS sequence_name
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'S'
AND n.nspname = 'public' -- 根据需要修改模式名
AND c.relname LIKE 'your_table%';
SELECT c.relname AS sequence_name
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'S'
AND n.nspname = 'public' -- 根据需要修改模式名
AND c.relname LIKE 'your_table%';

1.2 解决方式

sql
#查看最大id
SELECT MAX(id) FROM "document";

#修改
SELECT setval('your_table_id_seq', (SELECT MAX(id) FROM your_table) + 1);
#查看最大id
SELECT MAX(id) FROM "document";

#修改
SELECT setval('your_table_id_seq', (SELECT MAX(id) FROM your_table) + 1);