New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resetting autoincrement and truncate with cascade #1506
Comments
Simply truncate the table |
Cheers! |
Sadly that doesn't work for postgres with the error:
Postgres apparently requires you to truncate all referenced tables in the same query:
Perhaps |
I think that even MySQL will raise that error, because of foreign keys constraints.
Here is an example using knex migrations // alter the table containing the foreign key
table.foreign('fk').references('pk').inTable('table_name').onDelete('CASCADE') |
That didn't help I'm afraid. To reiterate, the problem is not with deleting from a table which has a FK, the problem is deleting from a table which is referenced by a foreign key. So in my example table2 has a reference to table1.
|
Indeed it would make sense to use If there are none, then here is the place to append it. If there are any, I'd rather see a boolean flag to the function itself indicating to run with I definitely don't think a query building library with a |
Yeah, you'd want it because a potentially destructive operation shouldn't be the default behavior.
That sounds like it'd be fine. |
I solved using knex.raw knex.raw('ALTER TABLE ' + 'your_table' + ' AUTO_INCREMENT = 1'); |
I had the same problem for my tests. I solved it by using knex.raw also, not in the seed files but in my helper functions for unit testing :
After all, I only need it for tests in order to have manageable ids to assert against. |
I'm having an issue with this too. I'm using postgres for my database and to seed it I use:
however since adding another table that references the user table I've been getting the error
The relevant part of my new table migration looks like this:
So I changed to using I reckon a |
I did it in the seed with this...
if you are on node 7 and above, use async and await
|
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>
|
You are right, my solution in the comment is for mysql, others does not work as the reset is db specific |
If you are using mysql, you can specify mysql not to check foreign key constraint before truncating. const truncateKeyConstraint = (table, cb) => knex
.raw('SET foreign_key_checks = 0')
.raw('TRUNCATE table '+table)
.raw('SET foreign_key_checks = 1')
.then(() => cb())
.catch(err => cb(err)) |
I am using a SQLite 3 database and it seems like truncate() also resets the ID count: const config = require(`${process.cwd()}/knexfile`)[process.env.NODE_ENV]
const knex = require('knex')(config)
const {MyPOJO} = require('../src/MyPOJO')
const {Model} = require('objection')
beforeAll(async done => {
await knex.migrate.latest()
await knex(MyPOJO.tableName).truncate()
Model.knex(knex)
done()
}) |
@bennyn indeed it does. Currently this issue is about adding option for truncate with cascade. |
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') |
I found a knex helper lib that support PostgreSQL, MySQL and SQLite3: knex-cleaner |
Any update on this? |
@advename No progress. PRs most welcome! |
|
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()
asknex('table_name').del().resetIncrements()
which effectively resets a table to the state it was in when migrations were run.The text was updated successfully, but these errors were encountered: