question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resetting autoincrement and truncate with cascade

See original GitHub issue

Does knex provide any abstracted way of resetting autoincrement for a table? From what I can tell it can only be done using knex.raw() which makes it dependent on the dialect used.

A .resetIncrements() call would be nice so that it can be chained with .del() as knex('table_name').del().resetIncrements() which effectively resets a table to the state it was in when migrations were run.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

29reactions
ianwaltercommented, Dec 13, 2018

For other Postgres users looking for a solution to this, I’ve found that this works well:

await knex.raw('TRUNCATE TABLE my_table RESTART IDENTITY CASCADE')
9reactions
ultroxcommented, Aug 25, 2017

thanks @ais-one, you lead me to good path. I would just want to add, your sintax is database specific, and to another guy reading this, @ais-one solution might not work for you, dependint what db you are using.

To reset id in Postgres you need to specify <YOUTtableNAME_id_seq> It looks like this, usually do it after I delete all the tables.

await knex.raw('ALTER SEQUENCE requests_id_seq RESTART WITH 1')

await knex.raw('ALTER SEQUENCE profiles_id_seq RESTART WITH 1')

await knex.raw('ALTER SEQUENCE accounts_id_seq RESTART WITH 1')

await knex.raw('ALTER SEQUENCE cookies_id_seq RESTART WITH 1')

await knex.raw('ALTER SEQUENCE emails_id_seq RESTART WITH 1')

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL - Truncate a table on cascade and reset all ...
It work only for one sequence, but my problem is to restart all the sequence of the truncated tables. Consider when I use...
Read more >
Reset auto increment counter in postgres - sql - Stack Overflow
The following command does this automatically for you: This will also delete all the data in the table. So be careful. TRUNCATE TABLE...
Read more >
Reset Auto-Increment IDs in Postgres - Brian Childress
Sometimes that data is in a table with an auto-incrementing ID and I use the TRUNCATE command to remove all data and reset...
Read more >
Truncate a table and restart Sequences (Identity and cascade ...
But the above command doesn't restart the sequences i.e; IDENTITY and CASCADE associated with table columns. One Option is: TRUNCATE ...
Read more >
MySQL Reset Auto-Increment - Javatpoint
Similar to the TRUNCATE TABLE query, the pair of DROP TABLE and CREATE TABLE statements first drop the table and then recreate it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found