I use SELECT lastval() to get wrong serial id after truncated the table.
when I truncate the table, I use SELECT lastval(), I got the wrong ID/
If specified, all sequences on the truncated tables will be continue and not be reset. This is the default behavior. Optional.
TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.
The truncate command only removes all rows of a table. It does not remove the columns, indexes, constraints, and schema.
Following is the standard way to reset sequence:
truncate table table_name restart identity;
but in some version & platform, it's syntax error,
in that case, you can truncate without reset sequence, and alter the sequence with another sql, try this:
truncate table table_name; alter sequence seq_name start 1;
Use the TRUNCATE
SQL command.
For a single table the syntax is the following:
TRUNCATE TABLE table_name RESTART IDENTITY;
For multiple tables:
TRUNCATE TABLE table_foo, table_bar RESTART IDENTITY;
What it does:
Automatically restart sequences owned by columns of the truncated table(s).
Details here: TRUNCATE @ postgresql.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With