MCPcopy
hub / github.com/sequelize/sequelize / truncate

Method truncate

src/sequelize.js:876–900  ·  view source on GitHub ↗

* Truncate all tables defined through the sequelize models. * This is done by calling `Model.truncate()` on each model. * * @param {object} [options] The options passed to Model.destroy in addition to truncate * @param {boolean|Function} [options.logging] A function that logs sql queries

(options)

Source from the content-addressed store, hash-verified

874 * {@link Model.truncate} for more information
875 */
876 async truncate(options) {
877 const sortedModels = this.modelManager.getModelsTopoSortedByForeignKey();
878 const models = sortedModels || this.modelManager.models;
879 const hasCyclicDependencies = sortedModels == null;
880
881 // we have cyclic dependencies, cascade must be enabled.
882 if (hasCyclicDependencies && (!options || !options.cascade)) {
883 throw new Error('Sequelize#truncate: Some of your models have cyclic references (foreign keys). You need to use the "cascade" option to be able to delete rows from models that have cyclic references.');
884 }
885
886 // TODO [>=7]: throw if options.cascade is specified but unsupported in the given dialect.
887 if (hasCyclicDependencies && this.dialect.name === 'sqlite') {
888 // Workaround: SQLite does not support options.cascade, but we can disable its foreign key constraints while we
889 // truncate all tables.
890 return withSqliteForeignKeysOff(this, options, async () => {
891 await Promise.all(models.map(model => model.truncate(options)));
892 });
893 }
894
895 if (options && options.cascade) {
896 for (const model of models) await model.truncate(options);
897 } else {
898 await Promise.all(models.map(model => model.truncate(options)));
899 }
900 }
901
902 /**
903 * Drop all tables defined through this sequelize instance.

Callers

nothing calls this directly

Calls 3

withSqliteForeignKeysOffFunction · 0.85
allMethod · 0.80

Tested by

no test coverage detected