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.

sqlite: restartIdentity has no effect in Model.truncate

See original GitHub issue

Dialect library version: sqlite3 Sequelize version: 5.8.12 Node Version: v10.15.0 OS: Ubuntu 18.04

restartIdentity seems to have no effect in Model.truncate - primaryKey keeps incrementing from final value.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
victorhsanjoscommented, Apr 27, 2020

I was reading this post and tried to implement the truncate utility to clean my database before run tests, but no have success.

So I modified the truncate utility to work well with sqlite.

'use strict';

const models = require('./../../src/app/models');

module.exports = async () => {
  return await Promise.all(
    Object.keys(models).map((key) => {
      if (['sequelize', 'Sequelize'].includes(key)) return null;

      if (models.sequelize.getDialect() === 'sqlite') {
        models.sequelize.query('PRAGMA foreign_keys = OFF;');
        models.sequelize.query(`DELETE FROM ${models[key].tableName};`);
        models.sequelize.query(
          `DELETE FROM sqlite_sequence WHERE name = '${models[key].tableName}';`
        );

        return true;
      }

      return models[key].truncate({ restartIdentity: true });
    })
  );
};
1reaction
amolpatravali97commented, May 16, 2020

I was reading this post and tried to implement the truncate utility to clean my database before run tests, but no have success.

So I modified the truncate utility to work well with sqlite.

'use strict';

const models = require('./../../src/app/models');

module.exports = async () => {
  return await Promise.all(
    Object.keys(models).map((key) => {
      if (['sequelize', 'Sequelize'].includes(key)) return null;

      if (models.sequelize.getDialect() === 'sqlite') {
        models.sequelize.query('PRAGMA foreign_keys = OFF;');
        models.sequelize.query(`DELETE FROM ${models[key].tableName};`);
        models.sequelize.query(
          `DELETE FROM sqlite_sequence WHERE name = '${models[key].tableName}';`
        );

        return true;
      }

      return models[key].truncate({ restartIdentity: true });
    })
  );
};

Thanks, this works!

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLAlchemy, clear database content but don't drop the schema
I just want to erase all the data but leave the tables intact. Is this possible? The application uses Postgres and this is...
Read more >
SQLite: TRUNCATE TABLE Statement - TechOnTheNet
The TRUNCATE TABLE statement is used to remove all records from a table. SQLite does not have an explicit TRUNCATE TABLE command like...
Read more >
Model - Sequelize
Truncate all instances of the model. ... Returns true if this instance has not yet been persisted to the database ... (sqlite not...
Read more >
The database abstraction layer - Web2py
db is not a keyword; it is a local variable that stores the connection object DAL . ... Using web2py's model directory for...
Read more >
By using date (): date () method of datetime module returns ...
TRUNCATE RESTART IDENTITY ; Where is the name of the table. ... Jul 21, 2016 · 3 A bigint is not "a timestamp",...
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