MCPcopy
hub / github.com/sequelize/sequelize / _injectAttributes

Method _injectAttributes

lib/associations/belongs-to-many.js:259–395  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

257 // the id is in the target table
258 // or in an extra table which connects two tables
259 _injectAttributes() {
260 this.identifier = this.foreignKey;
261 this.foreignIdentifier = this.otherKey;
262
263 // remove any PKs previously defined by sequelize
264 // but ignore any keys that are part of this association (#5865)
265 _.each(this.through.model.rawAttributes, (attribute, attributeName) => {
266 if (attribute.primaryKey === true && attribute._autoGenerated === true) {
267 if ([this.foreignKey, this.otherKey].includes(attributeName)) {
268 // this key is still needed as it's part of the association
269 // so just set primaryKey to false
270 attribute.primaryKey = false;
271 }
272 else {
273 delete this.through.model.rawAttributes[attributeName];
274 }
275 this.primaryKeyDeleted = true;
276 }
277 });
278
279 const sourceKey = this.source.rawAttributes[this.sourceKey];
280 const sourceKeyType = sourceKey.type;
281 const sourceKeyField = this.sourceKeyField;
282 const targetKey = this.target.rawAttributes[this.targetKey];
283 const targetKeyType = targetKey.type;
284 const targetKeyField = this.targetKeyField;
285 const sourceAttribute = { type: sourceKeyType, ...this.foreignKeyAttribute };
286 const targetAttribute = { type: targetKeyType, ...this.otherKeyAttribute };
287
288 if (this.primaryKeyDeleted === true) {
289 targetAttribute.primaryKey = sourceAttribute.primaryKey = true;
290 } else if (this.through.unique !== false) {
291 let uniqueKey;
292 if (typeof this.options.uniqueKey === 'string' && this.options.uniqueKey !== '') {
293 uniqueKey = this.options.uniqueKey;
294 } else {
295 uniqueKey = [this.through.model.tableName, this.foreignKey, this.otherKey, 'unique'].join('_');
296 }
297 targetAttribute.unique = sourceAttribute.unique = uniqueKey;
298 }
299
300 if (!this.through.model.rawAttributes[this.foreignKey]) {
301 this.through.model.rawAttributes[this.foreignKey] = {
302 _autoGenerated: true
303 };
304 }
305
306 if (!this.through.model.rawAttributes[this.otherKey]) {
307 this.through.model.rawAttributes[this.otherKey] = {
308 _autoGenerated: true
309 };
310 }
311
312 if (this.options.constraints !== false) {
313 sourceAttribute.references = {
314 model: this.source.getTableName(),
315 key: sourceKeyField
316 };

Callers 2

belongsToManyFunction · 0.95
constructorMethod · 0.45

Calls 2

getTableNameMethod · 0.80
refreshAttributesMethod · 0.80

Tested by

no test coverage detected