MCPcopy
hub / github.com/sequelize/sequelize / HasMany

Class HasMany

lib/associations/has-many.js:17–497  ·  view source on GitHub ↗

* One-to-many association * * In the API reference below, add the name of the association to the method, e.g. for `User.hasMany(Project)` the getter will be `user.getProjects()`. * If the association is aliased, use the alias instead, e.g. `User.hasMany(Project, { as: 'jobs' })` will be `user.get

Source from the content-addressed store, hash-verified

15 * @see {@link Model.hasMany}
16 */
17class 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]) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected