MCPcopy Index your code
hub / github.com/sequelize/sequelize / delete

Method delete

src/dialects/abstract/query-interface.js:933–964  ·  view source on GitHub ↗
(instance, tableName, identifier, options)

Source from the content-addressed store, hash-verified

931 }
932
933 async delete(instance, tableName, identifier, options) {
934 const cascades = [];
935 const sql = this.queryGenerator.deleteQuery(tableName, identifier, {}, instance.constructor);
936
937 options = { ...options };
938
939 // Check for a restrict field
940 if (!!instance.constructor && !!instance.constructor.associations) {
941 const keys = Object.keys(instance.constructor.associations);
942 const length = keys.length;
943 let association;
944
945 for (let i = 0; i < length; i++) {
946 association = instance.constructor.associations[keys[i]];
947 if (association.options && association.options.onDelete &&
948 association.options.onDelete.toLowerCase() === 'cascade' &&
949 association.options.useHooks === true) {
950 cascades.push(association.accessors.get);
951 }
952 }
953 }
954
955 for (const cascade of cascades) {
956 let instances = await instance[cascade](options);
957 // Check for hasOne relationship with non-existing associate ("has zero")
958 if (!instances) continue;
959 if (!Array.isArray(instances)) instances = [instances];
960 for (const _instance of instances) await _instance.destroy(options);
961 }
962 options.instance = instance;
963 return await this.sequelize.query(sql, options);
964 }
965
966 /**
967 * Delete multiple records from a table

Callers 5

support.jsFile · 0.80
testFunction · 0.80
delete.test.jsFile · 0.80
changedMethod · 0.80
destroyMethod · 0.80

Calls 3

destroyMethod · 0.80
deleteQueryMethod · 0.45
queryMethod · 0.45

Tested by 1

testFunction · 0.64