MCPcopy
hub / github.com/sequelize/sequelize / sync

Method sync

lib/model.js:1293–1384  ·  lib/model.js::Model.sync

* Sync this Model to the DB, that is create the table. * * @param {object} [options] sync options * * @see * {@link Sequelize#sync} for options * * @returns {Promise<Model>}

(options)

Source from the content-addressed store, hash-verified

1291 * @returns {Promise<Model>}
1292 */
1293 static async sync(options) {
1294 options = { ...this.options, ...options };
1295 options.hooks = options.hooks === undefined ? true : !!options.hooks;
1296
1297 const attributes = this.tableAttributes;
1298 const rawAttributes = this.fieldRawAttributesMap;
1299
1300 if (options.hooks) {
1301 await this.runHooks(class="st">'beforeSync', options);
1302 }
1303 if (options.force) {
1304 await this.drop(options);
1305 }
1306
1307 const tableName = this.getTableName(options);
1308
1309 await this.queryInterface.createTable(tableName, attributes, options, this);
1310
1311 if (options.alter) {
1312 const tableInfos = await Promise.all([
1313 this.queryInterface.describeTable(tableName, options),
1314 this.queryInterface.getForeignKeyReferencesForTable(tableName, options)
1315 ]);
1316 const columns = tableInfos[0];
1317 class="cm">// Use for alter foreign keys
1318 const foreignKeyReferences = tableInfos[1];
1319 const removedConstraints = {};
1320
1321 for (const columnName in attributes) {
1322 if (!Object.prototype.hasOwnProperty.call(attributes, columnName)) continue;
1323 if (!columns[columnName] && !columns[attributes[columnName].field]) {
1324 await this.queryInterface.addColumn(tableName, attributes[columnName].field || columnName, attributes[columnName], options);
1325 }
1326 }
1327
1328 if (options.alter === true || typeof options.alter === class="st">'object' && options.alter.drop !== false) {
1329 for (const columnName in columns) {
1330 if (!Object.prototype.hasOwnProperty.call(columns, columnName)) continue;
1331 const currentAttribute = rawAttributes[columnName];
1332 if (!currentAttribute) {
1333 await this.queryInterface.removeColumn(tableName, columnName, options);
1334 continue;
1335 }
1336 if (currentAttribute.primaryKey) continue;
1337 class="cm">// Check foreign keys. If it's a foreign key, it should remove constraint first.
1338 const references = currentAttribute.references;
1339 if (currentAttribute.references) {
1340 const database = this.sequelize.config.database;
1341 const schema = this.sequelize.config.schema;
1342 class="cm">// Find existed foreign keys
1343 for (const foreignKeyReference of foreignKeyReferences) {
1344 const constraintName = foreignKeyReference.constraintName;
1345 if (!!constraintName
1346 && foreignKeyReference.tableCatalog === database
1347 && (schema ? foreignKeyReference.tableSchema === schema : true)
1348 && foreignKeyReference.referencedTableName === references.model
1349 && foreignKeyReference.referencedColumnName === references.key
1350 && (schema ? foreignKeyReference.referencedTableSchema === schema : true)

Callers 15

sscce.jsFile · 0.45
error.test.jsFile · 0.45
schema.test.jsFile · 0.45
cls.test.jsFile · 0.45
trigger.test.jsFile · 0.45
data-types.test.jsFile · 0.45
testSuccessFunction · 0.45
timezone.test.jsFile · 0.45
utils.test.jsFile · 0.45
model.test.jsFile · 0.45

Calls 12

dropMethod · 0.95
getTableNameMethod · 0.95
allMethod · 0.80
addColumnMethod · 0.80
showIndexMethod · 0.80
addIndexMethod · 0.80
createTableMethod · 0.45
describeTableMethod · 0.45
removeColumnMethod · 0.45
removeConstraintMethod · 0.45
changeColumnMethod · 0.45

Tested by 8

testSuccessFunction · 0.36
verifyDeadlockFunction · 0.36
createUsersAndItemsFunction · 0.36
executeTestFunction · 0.36
checkTimezoneParsingFunction · 0.36
reloadDynamicOIDsFunction · 0.36