* Sync this Model to the DB, that is create the table. * * @param {object} [options] sync options * * @see * Sequelize#sync for options * * @returns {Promise }
(options)
| 1329 | * @returns {Promise<Model>} |
| 1330 | */ |
| 1331 | static async sync(options) { |
| 1332 | options = { ...this.options, ...options }; |
| 1333 | options.hooks = options.hooks === undefined ? true : !!options.hooks; |
| 1334 | |
| 1335 | const attributes = this.tableAttributes; |
| 1336 | const rawAttributes = this.fieldRawAttributesMap; |
| 1337 | |
| 1338 | if (options.hooks) { |
| 1339 | await this.runHooks('beforeSync', options); |
| 1340 | } |
| 1341 | |
| 1342 | const tableName = this.getTableName(options); |
| 1343 | |
| 1344 | let tableExists; |
| 1345 | if (options.force) { |
| 1346 | await this.drop(options); |
| 1347 | tableExists = false; |
| 1348 | } else { |
| 1349 | tableExists = await this.queryInterface.tableExists(tableName, options); |
| 1350 | } |
| 1351 | |
| 1352 | if (!tableExists) { |
| 1353 | await this.queryInterface.createTable(tableName, attributes, options, this); |
| 1354 | } else { |
| 1355 | // enums are always updated, even if alter is not set. createTable calls it too. |
| 1356 | await this.queryInterface.ensureEnums(tableName, attributes, options, this); |
| 1357 | } |
| 1358 | |
| 1359 | if (tableExists && options.alter) { |
| 1360 | const tableInfos = await Promise.all([ |
| 1361 | this.queryInterface.describeTable(tableName, options), |
| 1362 | this.queryInterface.getForeignKeyReferencesForTable(tableName, options) |
| 1363 | ]); |
| 1364 | |
| 1365 | const columns = tableInfos[0]; |
| 1366 | // Use for alter foreign keys |
| 1367 | const foreignKeyReferences = tableInfos[1]; |
| 1368 | const removedConstraints = {}; |
| 1369 | |
| 1370 | for (const columnName in attributes) { |
| 1371 | if (!Object.prototype.hasOwnProperty.call(attributes, columnName)) continue; |
| 1372 | if (!columns[columnName] && !columns[attributes[columnName].field]) { |
| 1373 | await this.queryInterface.addColumn(tableName, attributes[columnName].field || columnName, attributes[columnName], options); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (options.alter === true || typeof options.alter === 'object' && options.alter.drop !== false) { |
| 1378 | for (const columnName in columns) { |
| 1379 | if (!Object.prototype.hasOwnProperty.call(columns, columnName)) continue; |
| 1380 | const currentAttribute = rawAttributes[columnName]; |
| 1381 | if (!currentAttribute) { |
| 1382 | await this.queryInterface.removeColumn(tableName, columnName, options); |
| 1383 | continue; |
| 1384 | } |
| 1385 | if (currentAttribute.primaryKey) continue; |
| 1386 | // Check foreign keys. If it's a foreign key, it should remove constraint first. |
| 1387 | const references = currentAttribute.references; |
| 1388 | if (currentAttribute.references) { |