* 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)
| 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) |