(source, target, options)
| 16 | */ |
| 17 | class HasMany extends Association { |
| 18 | constructor(source, target, options) { |
| 19 | super(source, target, options); |
| 20 | |
| 21 | this.associationType = 'HasMany'; |
| 22 | this.targetAssociation = null; |
| 23 | this.sequelize = source.sequelize; |
| 24 | this.isMultiAssociation = true; |
| 25 | this.foreignKeyAttribute = {}; |
| 26 | |
| 27 | if (this.options.through) { |
| 28 | throw new Error('N:M associations are not supported with hasMany. Use belongsToMany instead'); |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * If self association, this is the target association |
| 33 | */ |
| 34 | if (this.isSelfAssociation) { |
| 35 | this.targetAssociation = this; |
| 36 | } |
| 37 | |
| 38 | if (this.as) { |
| 39 | this.isAliased = true; |
| 40 | |
| 41 | if (_.isPlainObject(this.as)) { |
| 42 | this.options.name = this.as; |
| 43 | this.as = this.as.plural; |
| 44 | } else { |
| 45 | this.options.name = { |
| 46 | plural: this.as, |
| 47 | singular: Utils.singularize(this.as) |
| 48 | }; |
| 49 | } |
| 50 | } else { |
| 51 | this.as = this.target.options.name.plural; |
| 52 | this.options.name = this.target.options.name; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Foreign key setup |
| 57 | */ |
| 58 | if (_.isObject(this.options.foreignKey)) { |
| 59 | this.foreignKeyAttribute = this.options.foreignKey; |
| 60 | this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName; |
| 61 | } else if (this.options.foreignKey) { |
| 62 | this.foreignKey = this.options.foreignKey; |
| 63 | } |
| 64 | |
| 65 | if (!this.foreignKey) { |
| 66 | this.foreignKey = Utils.camelize( |
| 67 | [ |
| 68 | this.source.options.name.singular, |
| 69 | this.source.primaryKeyAttribute |
| 70 | ].join('_') |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | if (this.target.rawAttributes[this.foreignKey]) { |
| 75 | this.identifierField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey; |
nothing calls this directly
no test coverage detected