* Add a constraint to a table * * Available constraints: * - UNIQUE * - DEFAULT (MSSQL only) * - CHECK (MySQL - Ignored by the database engine ) * - FOREIGN KEY * - PRIMARY KEY * * @example <caption>UNIQUE</caption> * queryInterface.addConstraint('Users', { * field
(tableName, options)
| 731 | * @returns {Promise} |
| 732 | */ |
| 733 | async addConstraint(tableName, options) { |
| 734 | if (!options.fields) { |
| 735 | throw new Error(class="st">'Fields must be specified through options.fields'); |
| 736 | } |
| 737 | |
| 738 | if (!options.type) { |
| 739 | throw new Error(class="st">'Constraint type must be specified through options.type'); |
| 740 | } |
| 741 | |
| 742 | options = Utils.cloneDeep(options); |
| 743 | |
| 744 | const sql = this.queryGenerator.addConstraintQuery(tableName, options); |
| 745 | return await this.sequelize.query(sql, options); |
| 746 | } |
| 747 | |
| 748 | async showConstraint(tableName, constraintName, options) { |
| 749 | const sql = this.queryGenerator.showConstraintsQuery(tableName, constraintName); |